001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.masterindex.plugin.datamodel;
042:
043: import java.io.FileInputStream;
044: import java.io.FileNotFoundException;
045: import java.io.InputStream;
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import net.java.hulp.i18n.Logger;
050: import org.netbeans.modules.etl.logger.Localizer;
051: import org.netbeans.modules.etl.logger.LogUtil;
052:
053: /**
054: * @author Sujit Biswas
055: * @author Manish Bharani
056: */
057: public class Lookup {
058:
059: private static transient final Logger mLogger = LogUtil
060: .getLogger(Lookup.class.getName());
061: private static transient final Localizer mLoc = Localizer.get();
062: private HashMap<String, HashMap<String, Integer>> lookupMap = new HashMap<String, HashMap<String, Integer>>();
063: private HashMap<String, Integer> childIndex = new HashMap<String, Integer>();
064: private transient ObjectDefinition objectdef;
065:
066: /**
067: * @param objectdef
068: */
069: private Lookup(ObjectDefinition objectdef) {
070: this .objectdef = objectdef;
071: createLookup();
072: }
073:
074: /**
075: *
076: * @param objectDefinition
077: */
078: private Lookup(InputStream objectDefinition) {
079: ObjectDefinitionBuilder b = new ObjectDefinitionBuilder();
080: this .objectdef = b.parse(objectDefinition);
081: createLookup();
082: }
083:
084: /**
085: * populate the lookupMap and the childIndex
086: *
087: */
088: private void createLookup() {
089: createLooupMap(lookupMap, objectdef, objectdef.getName());
090: createChildIndex(childIndex, objectdef, objectdef.getName());
091:
092: }
093:
094: /**
095: * populate the childIndex
096: *
097: */
098: private void createChildIndex(
099: HashMap<String, Integer> childTypeIndex,
100: ObjectDefinition context, String prefix) {
101: for (int i = 0; i < context.getChildren().size(); i++) {
102:
103: ObjectDefinition child = context.getChildren().get(i);
104: String cname = child.getName();
105: String key = prefix + "." + cname;
106: childTypeIndex.put(key, i);
107: createChildIndex(childTypeIndex, child, key);
108: }
109:
110: }
111:
112: /**
113: * populate the lookupMap
114: *
115: */
116: private void createLooupMap(
117: HashMap<String, HashMap<String, Integer>> lmap,
118: ObjectDefinition context, String prefix) {
119:
120: lmap.put(prefix, createFieldMap(context));
121:
122: for (int i = 0; i < context.getChildren().size(); i++) {
123:
124: ObjectDefinition child = context.getChildren().get(i);
125: String cname = child.getName();
126: String key = prefix + "." + cname;
127: createLooupMap(lmap, child, key);
128: }
129:
130: }
131:
132: /**
133: * create the field map for a given ObjectDefinition
134: *
135: * @param context
136: * @return
137: */
138: private HashMap<String, Integer> createFieldMap(
139: ObjectDefinition context) {
140:
141: HashMap<String, Integer> map = new HashMap<String, Integer>();
142:
143: ArrayList<Field> fields = context.getFields();
144:
145: for (int i = 0; i < fields.size(); i++) {
146: map.put(fields.get(i).getName(), i);
147: }
148: return map;
149: }
150:
151: /**
152: * utility method to create Lookup from objectDefinition InputStream
153: *
154: * @param objectDefinition
155: * @return
156: */
157: public static Lookup createLookup(InputStream objectDefinition) {
158:
159: Lookup l = new Lookup(objectDefinition);
160: return l;
161:
162: }
163:
164: /**
165: * utility method to create Lookup from objectDefinition Object
166: *
167: * @param objectDefinition
168: * @return
169: */
170: public static Lookup createLookup(ObjectDefinition objectDefinition) {
171: Lookup l = new Lookup(objectDefinition);
172: return l;
173:
174: }
175:
176: /**
177: * returns the field index for a given fieldName and prefix where given an
178: * ePath Person.Address.city , fieldName=city and prefix=Person.Address,
179: * will return -1 if the field does not exist in the object definition
180: *
181: * @param fieldName
182: * @param prefix
183: * @return
184: */
185: public int getFieldIndex(String fieldName, String prefix) {
186:
187: if (lookupMap.get(prefix) == null) {
188: return -1;
189: }
190:
191: Integer i = lookupMap.get(prefix).get(fieldName);
192:
193: if (i == null) {
194: return -1;
195: } else {
196: return i.intValue();
197: }
198:
199: }
200:
201: /**
202: * returns the child index for a given prefix can be Person.Address
203: *
204: * @param prefix
205: * @return
206: */
207: public int getChildTypeIndex(String prefix) {
208: Integer i = childIndex.get(prefix);
209:
210: if (i == null) {
211: return -1;
212: } else {
213: return i.intValue();
214: }
215: }
216:
217: public String getChildTypeName(int j) {
218: Iterator i = childIndex.keySet().iterator();
219: while (i.hasNext()) {
220: String key = (String) i.next();
221: if ((int) childIndex.get(key) == j) {
222: return key.substring(key.indexOf(".") + 1);
223: }
224: }
225: return null;
226: }
227:
228: public HashMap getChildIndexMap() {
229: return this .childIndex;
230: }
231:
232: public HashMap getLookupMap() {
233: return this .lookupMap;
234: }
235:
236: public ArrayList getFields(String tablename) {
237: // Look out for parent fields
238: if (this .objectdef.getName().equals(tablename)) {
239: return objectdef.getFields();
240: } else {
241: // Look out for children fields
242: for (int i = 0; i < objectdef.getChildren().size(); i++) {
243: ObjectDefinition odef = (ObjectDefinition) this .objectdef
244: .getchild(i);
245: if (odef.getName().equals(tablename)) {
246: return odef.getFields();
247: }
248: }
249: }
250:
251: mLogger
252: .infoNoloc(mLoc
253: .t(
254: "PRSR014: Fields for table [{0} cannot be found in the data model",
255: tablename));
256: return null;
257: }
258:
259: public String getRootName() {
260: return this .objectdef.getName();
261: }
262:
263: /**
264: * @param args
265: */
266: public static void main(String[] args) {
267:
268: try {
269: //FileInputStream fis = new FileInputStream("D:/temp/eviewconfig/objectdef.xml");
270: FileInputStream fis = new FileInputStream(
271: "D:/temp/forMANISH/objectdef.xml");
272: Lookup l = createLookup(fis);
273: } catch (FileNotFoundException e) {
274: // TODO Auto-generated catch block
275: e.printStackTrace();
276: }
277:
278: }
279: }
|