Coverage Report - org.apache.ojb.broker.query.UserAlias
 
Classes in this File Line Coverage Branch Coverage Complexity
UserAlias
N/A
N/A
2.125
 
 1  
 package org.apache.ojb.broker.query;
 2  
 
 3  
 /* Copyright 2002-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 import java.io.Serializable;
 19  
 import java.util.ArrayList;
 20  
 import java.util.HashMap;
 21  
 import java.util.Iterator;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.ojb.broker.util.logging.Logger;
 25  
 import org.apache.ojb.broker.util.logging.LoggerFactory;
 26  
 
 27  
 /**
 28  
  * This class is used to specify the path segments of a Criteria
 29  
  * that should have associated table aliases.  Previously, the default
 30  
  * behaviour was that all path segments participated in the alias
 31  
  * 
 32  
  * @author <a href="mailto:philip.warrick@mcgill.ca">Phil Warrick</a> 
 33  
  */
 34  
 public class UserAlias implements Serializable
 35  
 {
 36  
     private static final long serialVersionUID = 3257282552220627249L;
 37  
     
 38  
     private Map m_mapping = new HashMap();
 39  
     private String m_name = null;
 40  
     private String m_attributePath = null;
 41  
     private boolean m_allPathsAliased = false;
 42  
     private Logger m_logger = LoggerFactory.getLogger(UserAlias.class);
 43  
 
 44  
     /**
 45  
      * Constructor declaration
 46  
      *
 47  
      * @param name the name of the alias
 48  
      */
 49  
     public UserAlias(String name)
 50  
     {
 51  
         m_name = name;
 52  
     }
 53  
 
 54  
     /**
 55  
      * Constructor declaration
 56  
      *
 57  
      * @param name the name of the alias
 58  
      * @param attributePath the full path of the SelectionCriteria attribute
 59  
      * @param aliasPath the portion of the attributePath which should be aliased.
 60  
      * This should be unambiguous.  If ambiguous portions need aliasing (e.g.
 61  
      * B.C in allAs.B.C.B.C), use add() instead
 62  
      */
 63  
     public UserAlias(String name, String attributePath, String aliasPath)
 64  
     {
 65  
         m_name = name;
 66  
         m_attributePath = attributePath;
 67  
         if (attributePath.lastIndexOf(aliasPath) == -1)
 68  
         {
 69  
             m_logger.warn("aliasPath should be a substring of attributePath");
 70  
         }
 71  
         initMapping(attributePath, aliasPath);
 72  
     }
 73  
 
 74  
     /**
 75  
      * Constructor declaration
 76  
      *
 77  
      * @param name the name of the alias
 78  
      * @param attributePath the full path of the SelectionCriteria attribute
 79  
      * @param allPathsAliased indicates that all path portions of attributePath
 80  
      * should be aliased (previously was the default)
 81  
      */
 82  
     public UserAlias(String name, String attributePath, boolean allPathsAliased)
 83  
     {
 84  
         m_name = name;
 85  
         m_attributePath = attributePath;
 86  
         m_allPathsAliased = allPathsAliased;
 87  
     }
 88  
 
 89  
     /**
 90  
      * generates the mapping from the aliasPath
 91  
      * @param aliasPath the portion of attributePath which should be aliased
 92  
      *
 93  
      */
 94  
     private void initMapping(String attributePath, String aliasPath)
 95  
     {
 96  
         Iterator aliasSegmentItr = pathToSegments(aliasPath).iterator();
 97  
         String currPath = "";
 98  
         String separator = "";
 99  
         while (aliasSegmentItr.hasNext())
 100  
         {
 101  
             currPath = currPath + separator + (String) aliasSegmentItr.next();
 102  
             int beginIndex = attributePath.indexOf(currPath);
 103  
             if (beginIndex == -1)
 104  
             {
 105  
                 break;
 106  
             }
 107  
             int endIndex = beginIndex + currPath.length();
 108  
             m_mapping.put(attributePath.substring(0, endIndex), m_name);
 109  
             separator = ".";
 110  
         }
 111  
     }
 112  
 
 113  
     private ArrayList pathToSegments(String path)
 114  
     {
 115  
         ArrayList segments = new ArrayList();
 116  
         int sp = path.indexOf('.');
 117  
         while (sp != -1)
 118  
         {
 119  
             segments.add(path.substring(0, sp));
 120  
             path = path.substring(sp + 1);
 121  
             sp = path.indexOf('.');
 122  
         }
 123  
         segments.add(path);
 124  
         return segments;
 125  
     }
 126  
 
 127  
     /**
 128  
      * Returns the name of this alias
 129  
      */
 130  
     public String getName()
 131  
     {
 132  
         return m_name;
 133  
     }
 134  
 
 135  
     /**
 136  
      * Returns the name of this alias if path has been added
 137  
      * to the aliased portions of attributePath
 138  
      *
 139  
      * @param path the path to test for inclusion in the alias
 140  
      */
 141  
     public String getAlias(String path)
 142  
     {
 143  
         if (m_allPathsAliased && m_attributePath.lastIndexOf(path) != -1)
 144  
         {
 145  
             return m_name;
 146  
         }
 147  
         Object retObj = m_mapping.get(path);
 148  
         if (retObj != null)
 149  
         {
 150  
             return (String) retObj;
 151  
         }
 152  
         return null;
 153  
     }
 154  
 
 155  
     /**
 156  
      * Adds a path to the aliased paths
 157  
      *
 158  
      * @param path the path to add to the aliased paths
 159  
      */
 160  
     public void add(String path)
 161  
     {
 162  
         m_mapping.put(path, m_name);
 163  
     }
 164  
 }