001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049: /*
050: * DomainObjectHelper.java
051: *
052: * Created on March 30, 2003, 1:59 PM
053: */
054:
055: package org.jaffa.tools.patternmetaengine;
056:
057: import java.io.File;
058: import java.net.MalformedURLException;
059: import java.util.ArrayList;
060: import java.util.HashMap;
061: import java.util.Iterator;
062: import javax.xml.bind.JAXBContext;
063: import javax.xml.bind.UnmarshalException;
064: import javax.xml.bind.Unmarshaller;
065: import org.apache.log4j.Logger;
066: import org.jaffa.patterns.library.domain_creator_1_1.domain.*;
067: import org.jaffa.util.XmlHelper;
068: import java.util.List;
069:
070: /** This helper reads in all the domain object xml files fron the specified locations
071: * @author PaulE
072: * @version 1.0
073: */
074: public class DomainObjectHelper {
075: /** Set up Logging for Log4J */
076: private static Logger log = Logger
077: .getLogger(DomainObjectHelper.class);
078:
079: private HashMap m_domainMap = null;
080: private HashMap m_tableMap = null;
081: private ArrayList m_list = null;
082:
083: /** Creates a new instance of DomainObjectHelper
084: * This looks at the supplied directory (path) and creates a
085: * map of all domain objects in that package.
086: */
087: public DomainObjectHelper() {
088: m_domainMap = new HashMap();
089: m_tableMap = new HashMap();
090: m_list = new ArrayList();
091: }
092:
093: /** Loads domain object from the specified location. If location is a file this
094: * file is loaded, if the location is a directory, all files in there are loaded
095: * recusivly.
096: * @param file name of file or folder to load
097: */
098: public void loadObjects(String file) {
099: loadObjects(new File(file), true);
100: }
101:
102: /** Loads domain object from the specified location. If location is a file this
103: * file is loaded, if the location is a directory, all files in there are loaded
104: * recusivly, if specified.
105: * @param file file or location to load
106: * @param recursive if loaction is a folder should subfolder be loaded as well (set to true for a recursive load)
107: */
108: public void loadObjects(File file, boolean recursive) {
109: if (file == null)
110: return;
111: //log.debug("Looking At File - " + file.getAbsolutePath());
112: if (file.isFile()) {
113: // only process XML files
114: if (file.getAbsolutePath().toLowerCase().endsWith(".xml"))
115: loadObject(file);
116: } else if (file.isDirectory() && recursive) {
117: File[] files = file.listFiles();
118: for (int i = 0; i < files.length; i++)
119: loadObjects(files[i], recursive);
120: }
121: }
122:
123: private void loadObject(File file) {
124: if (file == null)
125: return;
126: //log.debug("Loading Domain Object File - " + file.getAbsolutePath());
127: try {
128: // create a JAXBContext capable of handling classes generated into the package
129: JAXBContext jc = JAXBContext
130: .newInstance("org.jaffa.patterns.library.domain_creator_1_1.domain");
131: // create an Unmarshaller
132: Unmarshaller u = jc.createUnmarshaller();
133: // enable validation
134: u.setValidating(true);
135:
136: // unmarshal a document into a tree of Java content objects composed of classes from the package.
137: Root domain = (Root) u.unmarshal(XmlHelper
138: .stripDoctypeDeclaration(file.toURL()));
139: // Add to the list
140: add(domain);
141: } catch (MalformedURLException e) {
142: log.error("Can't load file - " + file.getAbsolutePath()
143: + ", Bad URL");
144: } catch (UnmarshalException e) {
145: log.error("Invalid XML. File - " + file.getAbsolutePath(),
146: e);
147: } catch (Exception e) {
148: log.error("Unknown Error! Can't load file - "
149: + file.getAbsolutePath(), e);
150: }
151: }
152:
153: private void add(Root domainObject) {
154: m_domainMap.put(domainObject.getDomainObject(), domainObject);
155: m_tableMap.put(domainObject.getDatabaseTable(), domainObject);
156: m_list.add(domainObject);
157: log.debug("Added Domain Object. Name="
158: + domainObject.getDomainObject() + ", Table="
159: + domainObject.getDatabaseTable());
160: }
161:
162: /** Assume this is a remove from an object aquired from the iterator() methd */
163: private void remove(Root domainObject, Iterator list) {
164: m_domainMap.remove(domainObject.getDomainObject());
165: m_tableMap.remove(domainObject.getDatabaseTable());
166: list.remove();
167: log.debug("Removed Domain Object. Name="
168: + domainObject.getDomainObject() + ", Table="
169: + domainObject.getDatabaseTable());
170: }
171:
172: /** Get the domain object bean, based on the domain object name
173: * @param name domain object name
174: * @return root of domain object bean
175: */
176: public Root getByDomain(String name) {
177: return (Root) m_domainMap.get(name);
178: }
179:
180: /** Get the domain object bean, based on the database table name
181: * @param name name of table
182: * @return root of domain object bean
183: */
184: public Root getByTable(String name) {
185: return (Root) m_tableMap.get(name);
186: }
187:
188: /** Return an iterator for all domain object loaded
189: * @return iterator for all domain objects
190: */
191: public Iterator iterator() {
192: return m_list.iterator();
193: }
194:
195: /** This goes through each domain removing any field where ignore = true
196: * It then goes through each relationship and if any of the from or to fields
197: * of that relationship don't exits, the relationship is removed.
198: */
199: public void doIgnoreFilter() {
200: for (Iterator i = m_list.iterator(); i.hasNext();) {
201: Root domain = (Root) i.next();
202: doIgnoreFieldFilter(domain);
203:
204: // If there are no fields, remove this object from the list of valid objects
205: List fields = domain.getFields().getField();
206: if (fields == null || fields.isEmpty()) {
207: log.debug("Removed Domain Object "
208: + domain.getDomainObject()
209: + ", it has no fields!");
210: remove(domain, i);
211: }
212:
213: }
214: for (Iterator i = iterator(); i.hasNext();) {
215: Root domain = (Root) i.next();
216: doIgnoreRelationshipFilter(domain);
217: }
218: }
219:
220: public void doIgnoreFieldFilter(Root domain) {
221: List fields = domain.getFields().getField();
222: if (fields == null || fields.isEmpty()) {
223: return;
224: } else {
225: for (Iterator it1 = fields.iterator(); it1.hasNext();) {
226: org.jaffa.patterns.library.domain_creator_1_1.domain.Field fld = (org.jaffa.patterns.library.domain_creator_1_1.domain.Field) it1
227: .next();
228: if (fld.isIgnore()) {
229: it1.remove();
230: log.debug("Removed Field " + fld.getName()
231: + " from " + domain.getDomainObject());
232: }
233: }
234: }
235: }
236:
237: public void doIgnoreRelationshipFilter(Root domain) {
238: if (domain.getRelationships() == null)
239: return;
240: List rels = domain.getRelationships().getRelationship();
241: if (rels == null || rels.isEmpty()) {
242: return;
243: } else {
244: for (Iterator it1 = rels.iterator(); it1.hasNext();) {
245: org.jaffa.patterns.library.domain_creator_1_1.domain.Relationship rel = (org.jaffa.patterns.library.domain_creator_1_1.domain.Relationship) it1
246: .next();
247:
248: Root toDomain = getByDomain(rel.getToDomainObject());
249: if (toDomain == null) {
250: it1.remove();
251: log.debug("Removed Relationship "
252: + rel.getToDomainObject() + " from "
253: + domain.getDomainObject() + " -> "
254: + rel.getToDomainObject() + " Not Found!");
255: } else {
256:
257: // Check Keys, Loop through relationship fields
258: for (Iterator itFromFlds = rel.getFromFields()
259: .getRelationshipField().iterator(); itFromFlds
260: .hasNext();) {
261: String fromFld = ((RelationshipField) itFromFlds
262: .next()).getName();
263: if (!hasField(domain, fromFld)) {
264: it1.remove();
265: log.debug("Removed Relationship "
266: + rel.getToDomainObject()
267: + " from "
268: + domain.getDomainObject()
269: + " -> From Field " + fromFld
270: + " Not Found!");
271: toDomain = null;
272: break;
273: }
274: }
275: if (toDomain != null)
276: for (Iterator itToFlds = rel.getToFields()
277: .getRelationshipField().iterator(); itToFlds
278: .hasNext();) {
279: String toFld = ((RelationshipField) itToFlds
280: .next()).getName();
281: if (!hasField(toDomain, toFld)) {
282: it1.remove();
283: log.debug("Removed Relationship "
284: + rel.getToDomainObject()
285: + " from "
286: + domain.getDomainObject()
287: + " -> From Field " + toFld
288: + " Not Found!");
289: break;
290: }
291: }
292: }
293: }
294: // Remove if none are valid
295: if (rels.size() <= 0)
296: domain.setRelationships(null);
297: }
298: }
299:
300: public boolean hasField(Root domain, String name) {
301: List fields = domain.getFields().getField();
302: if (fields == null || fields.isEmpty()) {
303: return false;
304: } else {
305: for (Iterator it1 = fields.iterator(); it1.hasNext();) {
306: org.jaffa.patterns.library.domain_creator_1_1.domain.Field fld = (org.jaffa.patterns.library.domain_creator_1_1.domain.Field) it1
307: .next();
308: if (fld.getName().equals(name))
309: return true;
310: }
311: return false;
312: }
313:
314: }
315:
316: }
|