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