0001: /*
0002: *
0003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
0004: *
0005: * The contents of this file are subject to the SourceTap Public License
0006: * ("License"); You may not use this file except in compliance with the
0007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
0008: * Software distributed under the License is distributed on an "AS IS" basis,
0009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
0010: * the specific language governing rights and limitations under the License.
0011: *
0012: * The above copyright notice and this permission notice shall be included
0013: * in all copies or substantial portions of the Software.
0014: *
0015: */
0016:
0017: package com.sourcetap.sfa.ui;
0018:
0019: import java.io.BufferedWriter;
0020: import java.io.File;
0021: import java.io.FileWriter;
0022: import java.io.IOException;
0023: import java.io.PrintWriter;
0024: import java.net.URL;
0025: import java.util.ArrayList;
0026: import java.util.Collections;
0027: import java.util.HashMap;
0028: import java.util.Iterator;
0029: import java.util.LinkedList;
0030: import java.util.List;
0031: import java.util.Map;
0032:
0033: import org.ofbiz.base.util.Debug;
0034: import org.ofbiz.base.util.UtilMisc;
0035: import org.ofbiz.entity.GenericDelegator;
0036: import org.ofbiz.entity.GenericEntityException;
0037: import org.ofbiz.entity.GenericValue;
0038: import org.ofbiz.entity.model.ModelEntity;
0039: import org.ofbiz.entity.util.EntityDataLoader;
0040:
0041: /**
0042: * DOCUMENT ME!
0043: *
0044: */
0045: public class UIXMLFileUtil {
0046: public static final String module = UIXMLFileUtil.class.getName();
0047:
0048: /* public static void main (String[] argument) {
0049: if (argument.length != 3) {
0050: Debug.logError("Usage:", module);
0051: Debug.logError("UIGenerateSQL <party ID> <screen name like> <path>", module);
0052: return;
0053: }
0054: String partyId = argument[0];
0055: String screenName = argument[1];
0056: String filePath = argument[2];
0057: UIGenerateScreenSQL uIGenerateScreenSQL = new UIGenerateScreenSQL();
0058: GenericDelegator delegator = new GenericDelegator("UIGenerateScreenSQLDelegator");
0059: uIGenerateScreenSQL.writeUiScreenSql(delegator, partyId, screenName, filePath);
0060: return;
0061: }
0062: */
0063:
0064: /**
0065: * @param delegator
0066: * @param filePath - a file or directory or files to load
0067: * @return
0068: */
0069: public static int loadAll(GenericDelegator delegator,
0070: String filePath) throws GenericEntityException {
0071: List errorMessages = new ArrayList();
0072: String helperName = delegator.getGroupHelperName("org.ofbiz");
0073:
0074: List urlList = new ArrayList();
0075:
0076: File loadDir = new File(filePath);
0077: Debug.logVerbose("filepath = " + filePath, module);
0078: if (loadDir.exists() && loadDir.isDirectory()) {
0079: File[] files = loadDir.listFiles();
0080: List tempFileList = new LinkedList();
0081: for (int i = 0; i < files.length; i++) {
0082: if (files[i].getName().toLowerCase().endsWith(".xml")) {
0083: Debug.logVerbose("adding file "
0084: + files[i].getAbsoluteFile(), module);
0085: tempFileList.add(files[i]);
0086: }
0087: }
0088: Collections.sort(tempFileList);
0089: Iterator tempFileIter = tempFileList.iterator();
0090: while (tempFileIter.hasNext()) {
0091: File dataFile = (File) tempFileIter.next();
0092: if (dataFile.exists()) {
0093: URL url = null;
0094: try {
0095: url = dataFile.toURL();
0096: urlList.add(url);
0097: } catch (java.net.MalformedURLException e) {
0098: String xmlError = "Error loading XML file \""
0099: + dataFile.getAbsolutePath()
0100: + "\"; Error was: " + e.getMessage();
0101: Debug.logError(xmlError, module);
0102: }
0103: } else {
0104: String errorMsg = "Could not find file: \""
0105: + dataFile.getAbsolutePath() + "\"";
0106: Debug.logError(errorMsg, module);
0107: }
0108: }
0109: } else if (loadDir.exists() && loadDir.isFile()) {
0110: Debug.logVerbose("loading single file"
0111: + loadDir.getAbsolutePath(), module);
0112: URL url = null;
0113: try {
0114: url = loadDir.toURL();
0115: urlList.add(url);
0116: } catch (java.net.MalformedURLException e) {
0117: String xmlError = "Error loading XML file \""
0118: + loadDir.getAbsolutePath() + "\"; Error was: "
0119: + e.getMessage();
0120: Debug.logError(xmlError, module);
0121: }
0122: }
0123:
0124: int totalRowsChanged = 0;
0125: if (urlList.size() > 0) {
0126: Iterator urlIter = urlList.iterator();
0127: while (urlIter.hasNext()) {
0128: URL dataUrl = (URL) urlIter.next();
0129: int rowsChanged = EntityDataLoader.loadData(dataUrl,
0130: helperName, delegator, errorMessages);
0131: totalRowsChanged += rowsChanged;
0132: Debug.logError("Loaded " + rowsChanged + " rows from "
0133: + dataUrl.toExternalForm() + " ("
0134: + totalRowsChanged + " total rows so far)",
0135: module);
0136:
0137: }
0138: } else {
0139: Debug.logVerbose("No XML Files found.", module);
0140: }
0141:
0142: return totalRowsChanged;
0143: }
0144:
0145: public String saveAll(GenericDelegator delegator, String filePath) {
0146: List entityList = null;
0147: int numEntities = 0;
0148: String msg = "";
0149: String fileName = "";
0150:
0151: try {
0152: entityList = delegator.findAll("UiScreen");
0153: Iterator listIterator = entityList.iterator();
0154:
0155: while (listIterator.hasNext()) {
0156: GenericValue screen = (GenericValue) listIterator
0157: .next();
0158: numEntities++;
0159:
0160: String screenName = screen.getString("screenName");
0161: fileName = filePath + "/ui-s-"
0162: + screenName.replace(' ', '_') + ".xml";
0163: writeUiScreenFile(delegator, "-1", screen
0164: .getString("screenId"), fileName);
0165: }
0166:
0167: msg = numEntities + " screens saved<p>";
0168:
0169: fileName = filePath + "/ui-applications.xml";
0170: msg += writeAllRecords(delegator, "UiApplication", fileName);
0171:
0172: fileName = filePath + "/ui-layoutTypes.xml";
0173: msg += writeAllRecords(delegator, "UiLayoutType", fileName);
0174:
0175: entityList = delegator.findAll("UiDisplayObject");
0176: listIterator = entityList.iterator();
0177:
0178: while (listIterator.hasNext()) {
0179: GenericValue displayObject = (GenericValue) listIterator
0180: .next();
0181: numEntities++;
0182:
0183: String entityName = displayObject
0184: .getString("displayObjectId");
0185: fileName = filePath + "/ui-do-"
0186: + entityName.replace(' ', '_') + ".xml";
0187: writeUiDisplayObjectFile(delegator, entityName,
0188: fileName);
0189: }
0190:
0191: msg += numEntities + " display objects saved<p>";
0192:
0193: entityList = delegator.findAll("UiDisplayType");
0194: listIterator = entityList.iterator();
0195:
0196: while (listIterator.hasNext()) {
0197: GenericValue displayType = (GenericValue) listIterator
0198: .next();
0199: numEntities++;
0200:
0201: String entityName = displayType
0202: .getString("displayTypeId");
0203: fileName = filePath + "/ui-dt-"
0204: + entityName.replace(' ', '_') + ".xml";
0205: writeUiDisplayTypeFile(delegator, entityName, fileName);
0206: }
0207:
0208: msg += numEntities + " display types saved<p>";
0209:
0210: entityList = delegator.findAll("UiEntity");
0211: listIterator = entityList.iterator();
0212:
0213: while (listIterator.hasNext()) {
0214: GenericValue entity = (GenericValue) listIterator
0215: .next();
0216: numEntities++;
0217:
0218: String entityName = entity.getString("entityName");
0219: String entityId = entity.getString("entityId");
0220: fileName = filePath + "/ui-e-"
0221: + entityName.replace(' ', '_') + ".xml";
0222: writeUiEntityFile(delegator, entityId, fileName);
0223: }
0224:
0225: msg += numEntities + " entities saved<p>";
0226:
0227: entityList = delegator.findAll("CodeType");
0228: listIterator = entityList.iterator();
0229:
0230: while (listIterator.hasNext()) {
0231: GenericValue codeType = (GenericValue) listIterator
0232: .next();
0233: numEntities++;
0234:
0235: String entityName = codeType.getString("codeTypeId");
0236: fileName = filePath + "/code-"
0237: + entityName.replace(' ', '_') + ".xml";
0238: writeCodeTypeFile(delegator, entityName, fileName);
0239: }
0240:
0241: msg += numEntities + " codes saved<p>";
0242:
0243: entityList = delegator.findAll("UiReport");
0244: listIterator = entityList.iterator();
0245:
0246: while (listIterator.hasNext()) {
0247: GenericValue uiReport = (GenericValue) listIterator
0248: .next();
0249: numEntities++;
0250:
0251: String entityName = uiReport.getString("reportName");
0252: fileName = filePath + "/ui-rpt-"
0253: + entityName.replace(' ', '_') + ".xml";
0254: writeUiReportFile(delegator, uiReport
0255: .getString("reportId"), fileName);
0256: }
0257:
0258: msg += numEntities + " codes saved<p>";
0259:
0260: } catch (Exception e) {
0261: Debug
0262: .logError(
0263: "[UISQLFileWriter.saveAll]: Exception - Error was:",
0264: module);
0265: Debug.logError(e.getMessage(), module);
0266:
0267: return e.getMessage();
0268:
0269: }
0270:
0271: return msg;
0272:
0273: }
0274:
0275: public String writeUiScreenFile(GenericDelegator delegator,
0276: String partyId, String screenId, String filePath) {
0277: HashMap findHashMap = new HashMap();
0278: ArrayList findOrder = null;
0279: ModelEntity modelEntity = null;
0280: GenericValue genericValue = null;
0281: String entityName = "";
0282: String resultString = "";
0283:
0284: try {
0285: // Open the file, and replace any existing contents.
0286: FileWriter tempFile = new FileWriter(filePath, false);
0287: PrintWriter outputFile = new PrintWriter(
0288: new BufferedWriter(tempFile));
0289: writeHeader(outputFile);
0290:
0291: ////////////////////
0292: // Write out the SQL for inserting the screen.
0293: ////////////////////
0294: List screenGVL = null;
0295:
0296: screenGVL = writeSqlInserts("UiScreen", UtilMisc.toMap(
0297: "screenId", screenId), UtilMisc.toList("screenId"),
0298: delegator, outputFile);
0299:
0300: if (screenGVL.size() <= 0) {
0301: return "No screen found with ID \"" + screenId + "\"";
0302: } else {
0303: resultString += (String.valueOf(screenGVL.size()) + " screens<BR>\n");
0304: }
0305:
0306: ////////////////////
0307: // Write out the SQL for inserting the screen sections.
0308: ////////////////////
0309: // Loop through the screens. (Should be only one.)
0310: Iterator screenGVI = screenGVL.iterator();
0311: List screenSectionGVL = new LinkedList();
0312:
0313: while (screenGVI.hasNext()) {
0314: GenericValue screenGV = (GenericValue) screenGVI.next();
0315:
0316: screenSectionGVL.addAll(writeSqlInserts(
0317: "UiScreenSection", UtilMisc.toMap("screenId",
0318: screenGV.getString("screenId")),
0319: UtilMisc.toList("sectionId"), delegator,
0320: outputFile));
0321: }
0322:
0323: resultString += (String.valueOf(screenSectionGVL.size()) + " sections<BR>\n");
0324: screenGVI = null;
0325:
0326: ////////////////////
0327: // Write out the SQL for inserting the screen section entities and screen section infos (fields).
0328: ////////////////////
0329: Iterator screenSectionGVI = screenSectionGVL.iterator();
0330: List screenSectionEntityGVL = new LinkedList();
0331: List screenSectionInfoGVL = new LinkedList();
0332:
0333: while (screenSectionGVI.hasNext()) {
0334: GenericValue screenSectionGV = (GenericValue) screenSectionGVI
0335: .next();
0336:
0337: String sectionId = screenSectionGV
0338: .getString("sectionId");
0339:
0340: screenSectionEntityGVL.addAll(writeSqlInserts(
0341: "UiScreenSectionEntity", UtilMisc.toMap(
0342: "sectionId", sectionId), UtilMisc
0343: .toList("entityId"), delegator,
0344: outputFile));
0345:
0346: screenSectionEntityGVL.addAll(writeSqlInserts(
0347: "UiRelatedScreenSection", UtilMisc.toMap(
0348: "sectionId", sectionId), UtilMisc
0349: .toList("relatedScreenSectionId"),
0350: delegator, outputFile));
0351:
0352: screenSectionInfoGVL.addAll(writeSqlInserts(
0353: "UiScreenSectionInfo", UtilMisc.toMap(
0354: "sectionId", sectionId, "partyId",
0355: partyId), UtilMisc
0356: .toList("attributeId"), delegator,
0357: outputFile));
0358: }
0359:
0360: resultString += (String.valueOf(screenSectionEntityGVL
0361: .size()) + " section entities<BR>\n");
0362: resultString += (String
0363: .valueOf(screenSectionInfoGVL.size()) + " section fields");
0364: screenSectionGVI = null;
0365:
0366: writeFooter(outputFile);
0367: outputFile.close();
0368:
0369: return resultString;
0370: } catch (IOException e) {
0371: Debug
0372: .logError(
0373: "[UISQLFileWriter.writeUiScreenSql]: IO Exception - Error was:",
0374: module);
0375: Debug.logError(e.getMessage(), module);
0376:
0377: return e.getMessage();
0378: } catch (SecurityException se) {
0379: Debug
0380: .logError(
0381: "[UISQLFileWriter.writeUiScreenSql]: Security Exception - Error was:",
0382: module);
0383: Debug.logError(se.getMessage(), module);
0384:
0385: return se.getMessage();
0386: }
0387: }
0388:
0389: public String writeUiReportFile(GenericDelegator delegator,
0390: String reportId, String filePath) {
0391: HashMap findHashMap = new HashMap();
0392: ArrayList findOrder = null;
0393: ModelEntity modelEntity = null;
0394: GenericValue genericValue = null;
0395: String entityName = "";
0396: String resultString = "";
0397:
0398: try {
0399: // Open the file, and replace any existing contents.
0400: FileWriter tempFile = new FileWriter(filePath, false);
0401: PrintWriter outputFile = new PrintWriter(
0402: new BufferedWriter(tempFile));
0403: writeHeader(outputFile);
0404:
0405: ////////////////////
0406: // Write out the SQL for inserting the report.
0407: ////////////////////
0408: List reportGVL = null;
0409:
0410: reportGVL = writeSqlInserts("UiReport", UtilMisc.toMap(
0411: "reportId", reportId), UtilMisc.toList("reportId"),
0412: delegator, outputFile);
0413:
0414: if (reportGVL.size() <= 0) {
0415: return "No report found with ID \"" + reportId + "\"";
0416: } else {
0417: resultString += (String.valueOf(reportGVL.size()) + " reports<BR>\n");
0418: }
0419:
0420: ////////////////////
0421: // Write out the SQL for inserting the screen sections.
0422: ////////////////////
0423: // Loop through the reports. (Should be only one.)
0424: Iterator reportGVI = reportGVL.iterator();
0425:
0426: while (reportGVI.hasNext()) {
0427:
0428: ////////////////////
0429: // Write out the SQL for inserting the Fields, Conditions, and OrderBys.
0430: ////////////////////
0431: GenericValue reportGV = (GenericValue) reportGVI.next();
0432:
0433: writeSqlInserts("UiReportField", UtilMisc.toMap(
0434: "reportId", reportId), UtilMisc
0435: .toList("reportFieldId"), delegator, outputFile);
0436:
0437: writeSqlInserts("UiReportCriteria", UtilMisc.toMap(
0438: "reportId", reportId), UtilMisc
0439: .toList("reportCriteriaId"), delegator,
0440: outputFile);
0441:
0442: writeSqlInserts("UiReportOrderBy", UtilMisc.toMap(
0443: "reportId", reportId), UtilMisc
0444: .toList("reportOrderId"), delegator, outputFile);
0445: }
0446:
0447: writeFooter(outputFile);
0448: outputFile.close();
0449:
0450: return resultString;
0451: } catch (IOException e) {
0452: Debug
0453: .logError(
0454: "[UISQLFileWriter.writeUiScreenSql]: IO Exception - Error was:",
0455: module);
0456: Debug.logError(e.getMessage(), module);
0457:
0458: return e.getMessage();
0459: } catch (SecurityException se) {
0460: Debug
0461: .logError(
0462: "[UISQLFileWriter.writeUiScreenSql]: Security Exception - Error was:",
0463: module);
0464: Debug.logError(se.getMessage(), module);
0465:
0466: return se.getMessage();
0467: }
0468: }
0469:
0470: /**
0471: * DOCUMENT ME!
0472: *
0473: * @param delegator
0474: * @param displayObjectId
0475: * @param filePath
0476: *
0477: * @return
0478: */
0479: public String writeUiDisplayObjectFile(GenericDelegator delegator,
0480: String displayObjectId, String filePath) {
0481: HashMap findHashMap = new HashMap();
0482: ArrayList findOrder = null;
0483: ModelEntity modelEntity = null;
0484: GenericValue genericValue = null;
0485: String entityName = "";
0486: String resultString = "";
0487:
0488: try {
0489: // Open the file, and replace any existing contents.
0490: FileWriter tempFile = new FileWriter(filePath, false);
0491: PrintWriter outputFile = new PrintWriter(
0492: new BufferedWriter(tempFile));
0493: writeHeader(outputFile);
0494:
0495: ////////////////////
0496: // Write out the SQL for inserting the display object.
0497: ////////////////////
0498: List uiDisplayObjectGVL = null;
0499: findHashMap = new HashMap();
0500: findHashMap.put("displayObjectId", displayObjectId);
0501: findOrder = new ArrayList();
0502: findOrder.add("displayObjectId");
0503: uiDisplayObjectGVL = writeSqlInserts("UiDisplayObject",
0504: findHashMap, findOrder, delegator, outputFile);
0505:
0506: if (uiDisplayObjectGVL.size() <= 0) {
0507: return "No display object found with ID \""
0508: + displayObjectId + "\"";
0509: } else {
0510: resultString += (String.valueOf(uiDisplayObjectGVL
0511: .size()) + " display objects<BR>\n");
0512: }
0513:
0514: ////////////////////
0515: // Write out the SQL for inserting the display object attributes.
0516: ////////////////////
0517: // Loop through the display objects. (Should be only one).
0518: Iterator displayObjectGVI = uiDisplayObjectGVL.iterator();
0519: List uiDisplayObjectAttribGVL = new LinkedList();
0520:
0521: while (displayObjectGVI.hasNext()) {
0522: GenericValue uiDisplayObjectGV = (GenericValue) displayObjectGVI
0523: .next();
0524:
0525: findHashMap = new HashMap();
0526: findHashMap.put("displayObjectId", uiDisplayObjectGV
0527: .getString("displayObjectId"));
0528: findOrder = new ArrayList();
0529: findOrder.add("displayObjectId");
0530: findOrder.add("displayAttribId");
0531: uiDisplayObjectAttribGVL.addAll(writeSqlInserts(
0532: "UiDisplayObjectAttrib", findHashMap,
0533: findOrder, delegator, outputFile));
0534: }
0535:
0536: resultString += (String.valueOf(uiDisplayObjectAttribGVL
0537: .size()) + " display object attributes<BR>\n");
0538: displayObjectGVI = null;
0539:
0540: writeFooter(outputFile);
0541: outputFile.close();
0542:
0543: return resultString;
0544: } catch (IOException e) {
0545: Debug
0546: .logError(
0547: "[UISQLFileWriter.writeUiDisplayObjectSql]: IO Exception - Error was:",
0548: module);
0549: Debug.logError(e.getMessage(), module);
0550:
0551: return e.getMessage();
0552: } catch (SecurityException se) {
0553: Debug
0554: .logError(
0555: "[UISQLFileWriter.writeUiDisplayObjectSql]: Security Exception - Error was:",
0556: module);
0557: Debug.logError(se.getMessage(), module);
0558:
0559: return se.getMessage();
0560: }
0561: }
0562:
0563: /**
0564: * DOCUMENT ME!
0565: *
0566: * @param delegator
0567: * @param displayTypeId
0568: * @param filePath
0569: *
0570: * @return
0571: */
0572: public String writeUiDisplayTypeFile(GenericDelegator delegator,
0573: String displayTypeId, String filePath) {
0574: HashMap findHashMap = new HashMap();
0575: ArrayList findOrder = null;
0576: ModelEntity modelEntity = null;
0577: GenericValue genericValue = null;
0578: String entityName = "";
0579: String resultString = "";
0580:
0581: try {
0582: // Open the file, and replace any existing contents.
0583: FileWriter tempFile = new FileWriter(filePath, false);
0584: PrintWriter outputFile = new PrintWriter(
0585: new BufferedWriter(tempFile));
0586: writeHeader(outputFile);
0587:
0588: ////////////////////
0589: // Write out the SQL for inserting the display type.
0590: ////////////////////
0591: List uiDisplayTypeGVL = null;
0592: findHashMap = new HashMap();
0593: findHashMap.put("displayTypeId", displayTypeId);
0594: findOrder = new ArrayList();
0595: findOrder.add("displayTypeId");
0596: uiDisplayTypeGVL = writeSqlInserts("UiDisplayType",
0597: findHashMap, findOrder, delegator, outputFile);
0598:
0599: if (uiDisplayTypeGVL.size() <= 0) {
0600: return "No display type found with ID \""
0601: + displayTypeId + "\"";
0602: } else {
0603: resultString += (String
0604: .valueOf(uiDisplayTypeGVL.size()) + " display types<BR>\n");
0605: }
0606:
0607: ////////////////////
0608: // Write out the SQL for inserting the display attributes.
0609: ////////////////////
0610: // Loop through the display types. (Should be only one).
0611: Iterator displayTypeGVI = uiDisplayTypeGVL.iterator();
0612: List uiDisplayAttribGVL = new LinkedList();
0613:
0614: while (displayTypeGVI.hasNext()) {
0615: GenericValue displayTypeGV = (GenericValue) displayTypeGVI
0616: .next();
0617:
0618: findHashMap = new HashMap();
0619: findHashMap.put("displayTypeId", displayTypeGV
0620: .getString("displayTypeId"));
0621: findOrder = new ArrayList();
0622: findOrder.add("displayTypeId");
0623: findOrder.add("displayAttribId");
0624: uiDisplayAttribGVL.addAll(writeSqlInserts(
0625: "UiDisplayAttrib", findHashMap, findOrder,
0626: delegator, outputFile));
0627: }
0628:
0629: resultString += (String.valueOf(uiDisplayAttribGVL.size()) + " display attributes<BR>\n");
0630: displayTypeGVI = null;
0631:
0632: writeFooter(outputFile);
0633: outputFile.close();
0634:
0635: return resultString;
0636: } catch (IOException e) {
0637: Debug
0638: .logError(
0639: "[UISQLFileWriter.writeUiDisplayTypeSql]: IO Exception - Error was:",
0640: module);
0641: Debug.logError(e.getMessage(), module);
0642:
0643: return e.getMessage();
0644: } catch (SecurityException se) {
0645: Debug
0646: .logError(
0647: "[UISQLFileWriter.writeUiDisplayTypeSql]: Security Exception - Error was:",
0648: module);
0649: Debug.logError(se.getMessage(), module);
0650:
0651: return se.getMessage();
0652: }
0653: }
0654:
0655: /**
0656: * DOCUMENT ME!
0657: *
0658: * @param delegator
0659: * @param entityId
0660: * @param filePath
0661: *
0662: * @return
0663: */
0664: public String writeUiEntityFile(GenericDelegator delegator,
0665: String entityId, String filePath) {
0666: HashMap findHashMap = new HashMap();
0667: ArrayList findOrder = null;
0668: ModelEntity modelEntity = null;
0669: GenericValue genericValue = null;
0670: String entityName = "";
0671: String resultString = "";
0672:
0673: try {
0674: // Open the file, and replace any existing contents.
0675: FileWriter tempFile = new FileWriter(filePath, false);
0676: PrintWriter outputFile = new PrintWriter(
0677: new BufferedWriter(tempFile));
0678: writeHeader(outputFile);
0679:
0680: ////////////////////
0681: // Write out the SQL for inserting the UI entity.
0682: ////////////////////
0683: List uiEntityGVL = null;
0684: findHashMap = new HashMap();
0685: findHashMap.put("entityId", entityId);
0686: findOrder = new ArrayList();
0687: findOrder.add("entityId");
0688: uiEntityGVL = writeSqlInserts("UiEntity", findHashMap,
0689: findOrder, delegator, outputFile);
0690:
0691: if (uiEntityGVL.size() <= 0) {
0692: return "No entity found with ID \"" + entityId + "\"";
0693: } else {
0694: resultString += (String.valueOf(uiEntityGVL.size()) + " UI entities<BR>\n");
0695: }
0696:
0697: ////////////////////
0698: // Write out the SQL for inserting the UI attributes.
0699: ////////////////////
0700: // Loop through the UI entities. (Should be only one).
0701: Iterator uiEntityGVI = uiEntityGVL.iterator();
0702: List uiAttributeGVL = new LinkedList();
0703:
0704: while (uiEntityGVI.hasNext()) {
0705: GenericValue uiEntityGV = (GenericValue) uiEntityGVI
0706: .next();
0707:
0708: findHashMap = new HashMap();
0709: findHashMap.put("entityId", uiEntityGV
0710: .getString("entityId"));
0711: findOrder = new ArrayList();
0712: findOrder.add("attributeId");
0713: uiAttributeGVL.addAll(writeSqlInserts("UiAttribute",
0714: findHashMap, findOrder, delegator, outputFile));
0715: }
0716:
0717: resultString += (String.valueOf(uiAttributeGVL.size()) + " UI attributes<BR>\n");
0718: uiEntityGVI = null;
0719:
0720: writeFooter(outputFile);
0721: outputFile.close();
0722:
0723: return resultString;
0724: } catch (IOException e) {
0725: Debug
0726: .logError(
0727: "[UISQLFileWriter.writeUiEntitySql]: IO Exception - Error was:",
0728: module);
0729: Debug.logError(e.getMessage(), module);
0730:
0731: return e.getMessage();
0732: } catch (SecurityException se) {
0733: Debug
0734: .logError(
0735: "[UISQLFileWriter.writeUiEntitySql]: Security Exception - Error was:",
0736: module);
0737: Debug.logError(se.getMessage(), module);
0738:
0739: return se.getMessage();
0740: }
0741: }
0742:
0743: /**
0744: * DOCUMENT ME!
0745: *
0746: * @param delegator
0747: * @param entityId
0748: * @param filePath
0749: *
0750: * @return
0751: */
0752: public String writeAllRecords(GenericDelegator delegator,
0753: String entityName, String fileName) {
0754: ModelEntity modelEntity = null;
0755: GenericValue genericValue = null;
0756: String resultString = "";
0757:
0758: try {
0759: // Open the file, and replace any existing contents.
0760: FileWriter tempFile = new FileWriter(fileName, false);
0761: PrintWriter outputFile = new PrintWriter(
0762: new BufferedWriter(tempFile));
0763: writeHeader(outputFile);
0764:
0765: ////////////////////
0766: // Write out the SQL for inserting the UI entity.
0767: ////////////////////
0768: List uiEntityGVL = null;
0769: uiEntityGVL = writeSqlInserts(entityName, null, null,
0770: delegator, outputFile);
0771:
0772: if (uiEntityGVL.size() <= 0) {
0773: return "No rows found for \"" + entityName + "\"";
0774: } else {
0775: resultString += (String.valueOf(uiEntityGVL.size())
0776: + " " + entityName + " records saved<BR>\n");
0777: }
0778:
0779: writeFooter(outputFile);
0780: outputFile.close();
0781:
0782: return resultString;
0783: } catch (IOException e) {
0784: Debug
0785: .logError(
0786: "[UISQLFileWriter.writeUiEntitySql]: IO Exception - Error was:",
0787: module);
0788: Debug.logError(e.getMessage(), module);
0789:
0790: return e.getMessage();
0791: } catch (SecurityException se) {
0792: Debug
0793: .logError(
0794: "[UISQLFileWriter.writeUiEntitySql]: Security Exception - Error was:",
0795: module);
0796: Debug.logError(se.getMessage(), module);
0797:
0798: return se.getMessage();
0799: }
0800: }
0801:
0802: /**
0803: * DOCUMENT ME!
0804: *
0805: * @param delegator
0806: * @param entityId
0807: * @param filePath
0808: *
0809: * @return
0810: */
0811: public String writeLayoutTypeFile(GenericDelegator delegator,
0812: String layoutTypeId, String filePath) {
0813: HashMap findHashMap = new HashMap();
0814: ArrayList findOrder = null;
0815: ModelEntity modelEntity = null;
0816: GenericValue genericValue = null;
0817: String entityName = "";
0818: String resultString = "";
0819:
0820: try {
0821: // Open the file, and replace any existing contents.
0822: FileWriter tempFile = new FileWriter(filePath, false);
0823: PrintWriter outputFile = new PrintWriter(
0824: new BufferedWriter(tempFile));
0825: writeHeader(outputFile);
0826:
0827: ////////////////////
0828: // Write out the SQL for inserting the UI entity.
0829: ////////////////////
0830: List uiEntityGVL = null;
0831: findHashMap = new HashMap();
0832: findHashMap.put("layoutTypeId", layoutTypeId);
0833: findOrder = new ArrayList();
0834: findOrder.add("layoutTypeId");
0835: uiEntityGVL = writeSqlInserts("UiLayoutType", findHashMap,
0836: findOrder, delegator, outputFile);
0837:
0838: if (uiEntityGVL.size() <= 0) {
0839: return "No layoutType found with ID \"" + layoutTypeId
0840: + "\"";
0841: } else {
0842: resultString += (String.valueOf(uiEntityGVL.size()) + " Layout Types<BR>\n");
0843: }
0844:
0845: writeFooter(outputFile);
0846: outputFile.close();
0847:
0848: return resultString;
0849: } catch (IOException e) {
0850: Debug
0851: .logError(
0852: "[UISQLFileWriter.writeUiEntitySql]: IO Exception - Error was:",
0853: module);
0854: Debug.logError(e.getMessage(), module);
0855:
0856: return e.getMessage();
0857: } catch (SecurityException se) {
0858: Debug
0859: .logError(
0860: "[UISQLFileWriter.writeUiEntitySql]: Security Exception - Error was:",
0861: module);
0862: Debug.logError(se.getMessage(), module);
0863:
0864: return se.getMessage();
0865: }
0866: }
0867:
0868: /**
0869: * DOCUMENT ME!
0870: *
0871: * @param delegator
0872: * @param codeTypeId
0873: * @param filePath
0874: *
0875: * @return
0876: */
0877: public String writeCodeTypeFile(GenericDelegator delegator,
0878: String codeTypeId, String filePath) {
0879: HashMap findHashMap = new HashMap();
0880: ArrayList findOrder = null;
0881: ModelEntity modelEntity = null;
0882: GenericValue genericValue = null;
0883: String entityName = "";
0884: String resultString = "";
0885:
0886: try {
0887: // Open the file, and replace any existing contents.
0888: FileWriter tempFile = new FileWriter(filePath, false);
0889: PrintWriter outputFile = new PrintWriter(
0890: new BufferedWriter(tempFile));
0891: writeHeader(outputFile);
0892:
0893: ////////////////////
0894: // Write out the SQL for inserting the Code Type.
0895: ////////////////////
0896: List codeTypeGVL = null;
0897: findHashMap = new HashMap();
0898: findHashMap.put("codeTypeId", codeTypeId);
0899: findOrder = new ArrayList();
0900: findOrder.add("codeTypeId");
0901: codeTypeGVL = writeSqlInserts("CodeType", findHashMap,
0902: findOrder, delegator, outputFile);
0903:
0904: if (codeTypeGVL.size() <= 0) {
0905: return "No code type found with ID \"" + codeTypeId
0906: + "\"";
0907: } else {
0908: resultString += (String.valueOf(codeTypeGVL.size()) + " Code Types<BR>\n");
0909: }
0910:
0911: ////////////////////
0912: // Write out the SQL for inserting the Codes.
0913: ////////////////////
0914: // Loop through the UI Code Types. (Should be only one).
0915: Iterator codeTypeGVI = codeTypeGVL.iterator();
0916: List codeGVL = new LinkedList();
0917:
0918: while (codeTypeGVI.hasNext()) {
0919: GenericValue codeGV = (GenericValue) codeTypeGVI.next();
0920:
0921: findHashMap = new HashMap();
0922: findHashMap.put("codeTypeId", codeGV
0923: .getString("codeTypeId"));
0924: findOrder = new ArrayList();
0925: findOrder.add("codeId");
0926: codeGVL.addAll(writeSqlInserts("Code", findHashMap,
0927: findOrder, delegator, outputFile));
0928: }
0929:
0930: resultString += (String.valueOf(codeGVL.size()) + " Codes<BR>\n");
0931: codeTypeGVI = null;
0932:
0933: writeFooter(outputFile);
0934: outputFile.close();
0935:
0936: return resultString;
0937: } catch (IOException e) {
0938: Debug
0939: .logError(
0940: "[UISQLFileWriter.writeUiEntitySql]: IO Exception - Error was:",
0941: module);
0942: Debug.logError(e.getMessage(), module);
0943:
0944: return e.getMessage();
0945: } catch (SecurityException se) {
0946: Debug
0947: .logError(
0948: "[UISQLFileWriter.writeUiEntitySql]: Security Exception - Error was:",
0949: module);
0950: Debug.logError(se.getMessage(), module);
0951:
0952: return se.getMessage();
0953: }
0954: }
0955:
0956: /**
0957: * DOCUMENT ME!
0958: *
0959: * @param outputFile
0960: * @param outputLine
0961: */
0962: protected void writeLine(BufferedWriter outputFile,
0963: String outputLine) {
0964: try {
0965: outputFile.write(outputLine);
0966: outputFile.newLine();
0967: outputFile.flush();
0968:
0969: return;
0970: } catch (IOException e) {
0971: Debug
0972: .logError(
0973: "[UISQLFileWriter.writeUiScreenSql]: IO Exception - Error was:",
0974: module);
0975: Debug.logError(e.getMessage(), module);
0976: } catch (SecurityException se) {
0977: Debug
0978: .logError(
0979: "[UISQLFileWriter.writeUiScreenSql]: Security Exception - Error was:",
0980: module);
0981: Debug.logError(se.getMessage(), module);
0982: }
0983: }
0984:
0985: /**
0986: * DOCUMENT ME!
0987: *
0988: * @param entityName
0989: * @param findHashMap
0990: * @param findOrder
0991: * @param delegator
0992: * @param outputFile
0993: *
0994: * @return
0995: */
0996: public List writeSqlInserts(String entityName, Map findHashMap,
0997: List findOrder, GenericDelegator delegator,
0998: PrintWriter outputFile) {
0999: String fieldName = "";
1000: String fieldValue = "";
1001: int fieldCount = 0;
1002: String outputString = "";
1003: List genericValueList = null;
1004:
1005: try {
1006: ModelEntity modelEntity = delegator
1007: .getModelEntity(entityName);
1008: if (findHashMap == null)
1009: genericValueList = delegator.findAll(entityName,
1010: findOrder);
1011: else
1012: genericValueList = delegator.findByLike(entityName,
1013: findHashMap, findOrder);
1014:
1015: Iterator genericValueIterator = genericValueList.iterator();
1016:
1017: while (genericValueIterator.hasNext()) {
1018: GenericValue genericValue = (GenericValue) genericValueIterator
1019: .next();
1020:
1021: genericValue.writeXmlText(outputFile, "");
1022: }
1023:
1024: return genericValueList;
1025: } catch (GenericEntityException gee) {
1026: Debug
1027: .logError(
1028: "[UISQLFileWriter.writeSqlInserts]: Generic Entity Exception - Error was:",
1029: module);
1030: Debug.logError(gee.getMessage(), module);
1031:
1032: return genericValueList;
1033: }
1034: }
1035:
1036: private void writeHeader(PrintWriter outputFile) {
1037: outputFile
1038: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1039: outputFile.println("<entity-engine-xml>");
1040: }
1041:
1042: private void writeFooter(PrintWriter outputFile) {
1043: outputFile.println("</entity-engine-xml>");
1044: }
1045:
1046: }
|