001: package org.apache.ojb.broker.query;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.Serializable;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import org.apache.ojb.broker.util.logging.Logger;
025: import org.apache.ojb.broker.util.logging.LoggerFactory;
026:
027: /**
028: * This class is used to specify the path segments of a Criteria
029: * that should have associated table aliases. Previously, the default
030: * behaviour was that all path segments participated in the alias
031: *
032: * @author <a href="mailto:philip.warrick@mcgill.ca">Phil Warrick</a>
033: */
034: public class UserAlias implements Serializable {
035: private static final long serialVersionUID = 3257282552220627249L;
036:
037: private Map m_mapping = new HashMap();
038: private String m_name = null;
039: private String m_attributePath = null;
040: private boolean m_allPathsAliased = false;
041: private Logger m_logger = LoggerFactory.getLogger(UserAlias.class);
042:
043: /**
044: * Constructor declaration
045: *
046: * @param name the name of the alias
047: */
048: public UserAlias(String name) {
049: m_name = name;
050: }
051:
052: /**
053: * Constructor declaration
054: *
055: * @param name the name of the alias
056: * @param attributePath the full path of the SelectionCriteria attribute
057: * @param aliasPath the portion of the attributePath which should be aliased.
058: * This should be unambiguous. If ambiguous portions need aliasing (e.g.
059: * B.C in allAs.B.C.B.C), use add() instead
060: */
061: public UserAlias(String name, String attributePath, String aliasPath) {
062: m_name = name;
063: m_attributePath = attributePath;
064: if (attributePath.lastIndexOf(aliasPath) == -1) {
065: m_logger
066: .warn("aliasPath should be a substring of attributePath");
067: }
068: initMapping(attributePath, aliasPath);
069: }
070:
071: /**
072: * Constructor declaration
073: *
074: * @param name the name of the alias
075: * @param attributePath the full path of the SelectionCriteria attribute
076: * @param allPathsAliased indicates that all path portions of attributePath
077: * should be aliased (previously was the default)
078: */
079: public UserAlias(String name, String attributePath,
080: boolean allPathsAliased) {
081: m_name = name;
082: m_attributePath = attributePath;
083: m_allPathsAliased = allPathsAliased;
084: }
085:
086: /**
087: * generates the mapping from the aliasPath
088: * @param aliasPath the portion of attributePath which should be aliased
089: *
090: */
091: private void initMapping(String attributePath, String aliasPath) {
092: Iterator aliasSegmentItr = pathToSegments(aliasPath).iterator();
093: String currPath = "";
094: String separator = "";
095: while (aliasSegmentItr.hasNext()) {
096: currPath = currPath + separator
097: + (String) aliasSegmentItr.next();
098: int beginIndex = attributePath.indexOf(currPath);
099: if (beginIndex == -1) {
100: break;
101: }
102: int endIndex = beginIndex + currPath.length();
103: m_mapping.put(attributePath.substring(0, endIndex), m_name);
104: separator = ".";
105: }
106: }
107:
108: private ArrayList pathToSegments(String path) {
109: ArrayList segments = new ArrayList();
110: int sp = path.indexOf('.');
111: while (sp != -1) {
112: segments.add(path.substring(0, sp));
113: path = path.substring(sp + 1);
114: sp = path.indexOf('.');
115: }
116: segments.add(path);
117: return segments;
118: }
119:
120: /**
121: * Returns the name of this alias
122: */
123: public String getName() {
124: return m_name;
125: }
126:
127: /**
128: * Returns the name of this alias if path has been added
129: * to the aliased portions of attributePath
130: *
131: * @param path the path to test for inclusion in the alias
132: */
133: public String getAlias(String path) {
134: if (m_allPathsAliased
135: && m_attributePath.lastIndexOf(path) != -1) {
136: return m_name;
137: }
138: Object retObj = m_mapping.get(path);
139: if (retObj != null) {
140: return (String) retObj;
141: }
142: return null;
143: }
144:
145: /**
146: * Adds a path to the aliased paths
147: *
148: * @param path the path to add to the aliased paths
149: */
150: public void add(String path) {
151: m_mapping.put(path, m_name);
152: }
153: }
|