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: * BuildObjectFinder.java
051: *
052: * Created on March 30, 2003, 7:49 PM
053: */
054:
055: package org.jaffa.tools.patternmetaengine;
056:
057: import java.io.File;
058: import java.io.FileOutputStream;
059: import java.io.IOException;
060: import java.io.FileNotFoundException;
061: import java.util.List;
062: import java.util.Iterator;
063: import java.util.ArrayList;
064: import java.util.Properties;
065:
066: import javax.xml.bind.JAXBContext;
067: import javax.xml.bind.Validator;
068: import javax.xml.bind.ValidationException;
069: import javax.xml.bind.JAXBException;
070: import javax.xml.bind.Marshaller;
071: import javax.xml.bind.util.ValidationEventCollector;
072: import javax.xml.bind.ValidationEvent;
073:
074: import org.apache.log4j.Logger;
075: import org.jaffa.util.StringHelper;
076: import org.jaffa.datatypes.Defaults;
077: import org.jaffa.tools.patternmetaengine.domain.ApplicationBuilder;
078: import org.jaffa.tools.patternmetaengine.domain.Module;
079: import org.jaffa.patterns.library.domain_creator_1_1.domain.Field;
080: import org.jaffa.patterns.library.domain_creator_1_1.domain.Root;
081: import org.jaffa.patterns.library.object_finder_meta_2_0.domain.*;
082: import java.io.ByteArrayOutputStream;
083: import java.beans.XMLEncoder;
084:
085: /**
086: *
087: * @author PaulE
088: */
089: public class BuildObjectFinder_2 implements IBuilder {
090:
091: /** Set up Logging for Log4J */
092: private static Logger log = Logger
093: .getLogger(BuildObjectFinder.class);
094:
095: private ApplicationBuilder m_app = null;
096: private Module m_module = null;
097: private Root m_domain = null;
098:
099: private ObjectFinderMeta m_finder = null;
100: private Properties m_labels = null;
101:
102: private ComponentRegistry m_compReg = null;
103:
104: /** Creates a new instance of BuildObjectFinder */
105: public BuildObjectFinder_2(
106: ApplicationBuilder app,
107: ComponentRegistry comps,
108: Module module,
109: org.jaffa.patterns.library.domain_creator_1_1.domain.Root domain,
110: Properties labels) {
111:
112: log.debug("Create Finder For - " + domain.getDomainObject());
113: m_compReg = comps;
114: m_app = app;
115: m_module = module;
116: m_domain = domain;
117: m_labels = labels;
118: }
119:
120: /** Causes the meta data object to be build ready for saving.
121: * @param fullPackage If true then the .applications. and .modules. package names
122: * are used. If this is false, these are ommited for a much more condensed package
123: * naming convention
124: */
125: public void buildPhase1(boolean fullPackage) throws Exception {
126: ObjectFactory objFactory = new ObjectFactory();
127: try {
128: m_finder = objFactory.createObjectFinderMeta();
129: m_finder
130: .setPatternTemplate("patterns/library/object_finder_2_0/ObjectFinderPattern.xml"); // Mand
131: m_finder.setApplication(m_app.getApplicationName()); // Mand
132: m_finder.setModule(m_module.getName()); // Mand
133: m_finder
134: .setComponent(m_domain.getDomainObject() + "Finder"); // Mand
135: StringBuffer sb = new StringBuffer();
136: sb.append(m_app.getPackagePrefix());
137: sb.append(".");
138: if (fullPackage)
139: sb.append("applications.");
140: sb.append(m_app.getApplicationName());
141: sb.append(".");
142: if (fullPackage)
143: sb.append("modules.");
144: sb.append(m_module.getName());
145: m_finder.setBasePackage(sb.toString().toLowerCase()); // Mand
146: m_finder.setDomainObject(m_domain.getDomainObject()); // Mand
147: m_finder.setDomainPackage(m_domain.getDomainPackage()); // Mand
148:
149: // Create Labels for the screen titles
150: String labelDomain = m_domain.getLabelToken();// get it from the domain object or fabricate it!
151: if (labelDomain == null)
152: labelDomain = "[label."
153: + StringHelper.getUpper1(m_app
154: .getApplicationName())
155: + "."
156: + StringHelper.getUpper1(m_module.getName())
157: + "."
158: + StringHelper.getUpper1(m_domain
159: .getDomainObject()) + "]";
160: String labelId = "title."
161: + StringHelper
162: .getUpper1(m_app.getApplicationName())
163: + "."
164: + StringHelper.getUpper1(m_module.getName())
165: + "."
166: + StringHelper
167: .getUpper1(m_domain.getDomainObject())
168: + "Finder.";
169:
170: // Criteia Title
171: m_finder.setCriteriaTitle("[" + labelId + "criteria" + "]"); // Opt
172: m_labels.put(labelId + "criteria", labelDomain
173: + " Criteria");
174: // Results Title
175: m_finder.setResultsTitle("[" + labelId + "results" + "]"); // Opt
176: m_labels.put(labelId + "results", labelDomain + " List");
177: // Add consolidated title
178: m_finder.setConsolidatedCriteriaAndResultsTitle("["
179: + labelId + "consolidated" + "]"); // Opt;
180: m_labels.put(labelId + "consolidated", labelDomain
181: + " Inquiry");
182:
183: // Add in tiles customizations of specified
184: if (m_module.getModuleTilePrefix() != null
185: && m_module.getModuleTilePrefix().length() > 0) {
186: m_finder.setMainLayout(m_module.getModuleTilePrefix()
187: + ".MainLayout");
188: m_finder.setFinderLayout(m_module.getModuleTilePrefix()
189: + ".FinderLayout");
190: m_finder.setFinderExcelLayout(m_module
191: .getModuleTilePrefix()
192: + ".FinderExcelLayout");
193: m_finder.setFinderXmlLayout(m_module
194: .getModuleTilePrefix()
195: + ".FinderXmlLayout");
196: }
197:
198: // Add All Fields as Criteria Fields
199: List fields = m_domain.getFields().getField();
200: if (fields == null || fields.isEmpty()) {
201: log
202: .error("Domain Object "
203: + m_domain.getDomainObject()
204: + " has no fields, this is needed to build a valid finder");
205: } else {
206:
207: // Create Criteria Fields
208: CriteriaFields cfields = objFactory
209: .createCriteriaFields();
210: m_finder.setCriteriaFields(cfields);
211: for (Iterator it1 = fields.iterator(); it1.hasNext();) {
212: Field fld = (Field) it1.next();
213:
214: CriteriaField cfld = objFactory
215: .createCriteriaField();
216: cfields.getCriteriaField().add(cfld);
217:
218: cfld.setName(reservedName(fld.getName()));
219:
220: String dt = Defaults.getDataType(fld.getDataType());
221: if (dt == null)
222: throw new Exception(
223: "Can't Translate Java Class "
224: + fld.getDataType()
225: + " to a supported Data Type");
226: cfld.setDataType(dt);
227:
228: cfld.setDisplay(true);
229: cfld.setDomainField(fld.getName());
230: }
231:
232: // Create Results Fields
233: ResultsFields rfields = objFactory
234: .createResultsFields();
235: m_finder.setResultsFields(rfields);
236: for (Iterator it1 = fields.iterator(); it1.hasNext();) {
237: Field fld = (Field) it1.next();
238:
239: ResultsField rfld = objFactory.createResultsField();
240: rfields.getResultsField().add(rfld);
241:
242: rfld.setName(reservedName(fld.getName()));
243: String dt = Defaults.getDataType(fld.getDataType());
244: if (dt == null)
245: throw new Exception(
246: "Can't Translate Java Class "
247: + fld.getDataType()
248: + " to a supported Data Type");
249: rfld.setDataType(dt);
250:
251: rfld.setDisplay(true);
252: rfld
253: .setLabel(fld.getLabelToken() /*"[" + labelDomain + "." + fld.getName() + "]"*/);
254: rfld.setDomainField(fld.getName());
255: }
256:
257: // Create Key Fields
258: KeyFields kfields = objFactory.createKeyFields();
259: m_finder.setKeyFields(kfields);
260: for (Iterator it1 = fields.iterator(); it1.hasNext();) {
261: Field fld = (Field) it1.next();
262: if (fld.getPrimaryKey().equalsIgnoreCase("t")) {
263:
264: // This is a key field
265: KeyField kfld = objFactory.createKeyField();
266: kfields.getKeyField().add(kfld);
267:
268: kfld.setResultsFieldName(reservedName(fld
269: .getName()));
270: String dt = Defaults.getDataType(fld
271: .getDataType());
272: if (dt == null)
273: throw new Exception(
274: "Can't Translate Java Class "
275: + fld.getDataType()
276: + " to a supported Data Type");
277: kfld.setDataType(dt);
278: kfld
279: .setFieldNameInTargetComponent(reservedName(fld
280: .getName()));
281: }
282: }
283: }
284:
285: } catch (JAXBException e) {
286: log.error("Failed to create Finder Object");
287: }
288: }
289:
290: /** Saves the generated meta data to the prespecified location as an XML file
291: * NOTE: assumes that the build(..) method has been called!
292: */
293: public boolean save() {
294: String filename = m_app.getOutputRoot()
295: + m_app.getOutputFinders() +
296: /* m_module.getName().toLowerCase() + File.separator + */
297: m_domain.getDomainObject() + "Finder.xml";
298: File file = new File(filename);
299: File path = new File(file.getParent());
300: if (!path.exists())
301: path.mkdirs();
302:
303: // Create output stream
304: FileOutputStream out = null;
305: try {
306: try {
307: out = new FileOutputStream(file.getPath());
308: } catch (FileNotFoundException e) {
309: log.error("Failed to open output stream !", e);
310: return false;
311: }
312:
313: try {
314: // create a JAXBContext capable of handling classes generated into the package
315: JAXBContext jc = JAXBContext
316: .newInstance("org.jaffa.patterns.library.object_finder_meta_2_0.domain");
317: // create a Validator
318: Validator v = jc.createValidator();
319: ValidationEventCollector valErrors = new ValidationEventCollector();
320: v.setEventHandler(valErrors);
321: // validate the content tree
322: if (!v.validateRoot(m_finder)) {
323: log.error("Failed to validate Structure !");
324: JAXBHelper.showErrors(log, valErrors);
325:
326: // Display current XML
327: ByteArrayOutputStream s = new ByteArrayOutputStream();
328: XMLEncoder e = new XMLEncoder(s);
329: e.writeObject(m_finder);
330: e.close();
331: log
332: .error("Here is a XML representation of the invalid JavaBean...\n"
333: + s.toString());
334:
335: return false;
336: }
337:
338: // Write out XML document to file
339: Marshaller m = jc.createMarshaller();
340: JAXBHelper
341: .marshalWithDocType(
342: m,
343: m_finder,
344: out,
345: "Root",
346: "-//JAFFA//DTD Object Finder Meta 2.0//EN",
347: "http://jaffa.sourceforge.net/DTD/object-finder-meta_2_0.dtd");
348:
349: } catch (ValidationException e) {
350: log.error("Failed to validate Structure !", e);
351: return false;
352: } catch (JAXBException e) {
353: log
354: .error(
355: "Failed to marshal xml to output stream !",
356: e);
357: return false;
358: }
359: } finally {
360: if (out != null)
361: try {
362: out.close();
363: } catch (IOException e) {
364: }
365: }
366: return true;
367: }
368:
369: private static List reservedAttr = null;
370: static {
371: reservedAttr = new ArrayList();
372: reservedAttr.add("component");
373: }
374:
375: private String reservedName(String name) {
376: if (reservedAttr.contains(name.toLowerCase()))
377: return name + "1";
378: else
379: return name;
380: }
381:
382: public String getApplication() {
383: return m_finder.getApplication();
384: }
385:
386: public String getComponentControllerClass() {
387: return m_finder.getComponent() + "Component";
388: }
389:
390: public String getComponentControllerPackage() {
391: return m_finder.getBasePackage() + ".components."
392: + m_finder.getComponent().toLowerCase() + ".ui";
393: }
394:
395: public String getComponentName() {
396: return getModule() + "."
397: + StringHelper.getUpper1(m_finder.getComponent());
398: }
399:
400: public String getComponentType() {
401: return "Finder";
402: }
403:
404: public String getDomain() {
405: return m_finder.getDomainPackage() + "."
406: + m_finder.getDomainObject();
407: }
408:
409: public String getModule() {
410: return StringHelper.getUpper1(m_finder.getModule());
411: }
412:
413: public String getName() {
414: return StringHelper.getUpper1(m_finder.getComponent());
415: }
416:
417: /** Causes the meta data object to be build ready for saving.
418: * @param fullPackage If true then the .applications. and .modules. package names
419: * are used. If this is false, these are ommited for a much more condensed package
420: * naming convention
421: */
422: public void buildPhase2(boolean fullPackage) throws Exception {
423: ObjectFactory objFactory = new ObjectFactory();
424:
425: // Find The Viewer Component To Link To
426: IBuilder vcomp = m_compReg.findComponent(this .getDomain(),
427: "Viewer");
428: if (vcomp != null) {
429: Viewer viewer = objFactory.createViewer();
430: m_finder.setViewer(viewer);
431: viewer.setClassName(vcomp.getComponentControllerClass());
432: viewer.setComponentName(vcomp.getComponentName());
433: viewer.setPackage(vcomp.getComponentControllerPackage());
434: }
435:
436: // Find The Maintenance Component To Link To
437: IBuilder mcomp = m_compReg.findComponent(this .getDomain(),
438: "Maintenance");
439: if (mcomp != null) {
440: // Updator
441: Updator updator = objFactory.createUpdator();
442: m_finder.setUpdator(updator);
443: updator.setClassName(mcomp.getComponentControllerClass());
444: updator.setComponentName(mcomp.getComponentName());
445: updator.setPackage(mcomp.getComponentControllerPackage());
446: // Creator
447: Creator creator = objFactory.createCreator();
448: m_finder.setCreator(creator);
449: creator.setClassName(mcomp.getComponentControllerClass());
450: creator.setComponentName(mcomp.getComponentName());
451: creator.setPackage(mcomp.getComponentControllerPackage());
452: // Creator
453: Deletor deletor = objFactory.createDeletor();
454: m_finder.setDeletor(deletor);
455: deletor.setClassName(mcomp.getComponentControllerClass());
456: deletor.setComponentName(mcomp.getComponentName());
457: deletor.setPackage(mcomp.getComponentControllerPackage());
458: }
459:
460: }
461:
462: }
|