Source Code Cross Referenced for EntityDataLoader.java in  » ERP-CRM-Financial » ofbiz » org » ofbiz » entity » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » ofbiz » org.ofbiz.entity.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         *******************************************************************************/package org.ofbiz.entity.util;
019:
020:        import java.io.File;
021:        import java.net.URL;
022:        import java.util.Collection;
023:        import java.util.Collections;
024:        import java.util.Iterator;
025:        import java.util.LinkedList;
026:        import java.util.List;
027:        import java.util.StringTokenizer;
028:
029:        import org.w3c.dom.Element;
030:
031:        import org.ofbiz.base.component.ComponentConfig;
032:        import org.ofbiz.base.config.GenericConfigException;
033:        import org.ofbiz.base.config.MainResourceHandler;
034:        import org.ofbiz.base.config.ResourceHandler;
035:        import org.ofbiz.base.util.Debug;
036:        import org.ofbiz.base.util.UtilMisc;
037:        import org.ofbiz.entity.GenericDelegator;
038:        import org.ofbiz.entity.GenericEntityException;
039:        import org.ofbiz.entity.config.DatasourceInfo;
040:        import org.ofbiz.entity.config.EntityConfigUtil;
041:        import org.ofbiz.entity.config.EntityDataReaderInfo;
042:        import org.ofbiz.entity.model.ModelEntity;
043:        import org.ofbiz.entity.model.ModelReader;
044:        import org.ofbiz.entity.model.ModelUtil;
045:        import org.ofbiz.entity.model.ModelViewEntity;
046:
047:        /**
048:         * Some utility routines for loading seed data.
049:         */
050:        public class EntityDataLoader {
051:
052:            public static final String module = EntityDataLoader.class
053:                    .getName();
054:
055:            public static String getPathsString(String helperName) {
056:                StringBuffer pathBuffer = new StringBuffer();
057:                if (helperName != null && helperName.length() > 0) {
058:                    DatasourceInfo datasourceInfo = EntityConfigUtil
059:                            .getDatasourceInfo(helperName);
060:                    List sqlLoadPathElements = datasourceInfo.sqlLoadPaths;
061:                    Iterator slpIter = sqlLoadPathElements.iterator();
062:                    while (slpIter.hasNext()) {
063:                        Element sqlLoadPathElement = (Element) slpIter.next();
064:                        String prependEnv = sqlLoadPathElement
065:                                .getAttribute("prepend-env");
066:                        pathBuffer.append(pathBuffer.length() == 0 ? "" : ";");
067:                        if (prependEnv != null && prependEnv.length() > 0) {
068:                            pathBuffer.append(System.getProperty(prependEnv));
069:                            pathBuffer.append("/");
070:                        }
071:                        pathBuffer.append(sqlLoadPathElement
072:                                .getAttribute("path"));
073:                    }
074:                }
075:                return pathBuffer.toString();
076:            }
077:
078:            public static List getUrlList(String helperName) {
079:                DatasourceInfo datasourceInfo = EntityConfigUtil
080:                        .getDatasourceInfo(helperName);
081:                return getUrlList(helperName, null, datasourceInfo.readDatas);
082:            }
083:
084:            public static List getUrlList(String helperName,
085:                    String componentName) {
086:                DatasourceInfo datasourceInfo = EntityConfigUtil
087:                        .getDatasourceInfo(helperName);
088:                return getUrlList(helperName, componentName,
089:                        datasourceInfo.readDatas);
090:            }
091:
092:            public static List getUrlList(String helperName, List readerNames) {
093:                return getUrlList(helperName, null, readerNames);
094:            }
095:
096:            public static List getUrlList(String helperName,
097:                    String componentName, List readerNames) {
098:                String paths = getPathsString(helperName);
099:                List urlList = new LinkedList();
100:
101:                // first get files from resources
102:                if (readerNames != null) {
103:                    Iterator readDataIter = readerNames.iterator();
104:                    while (readDataIter.hasNext()) {
105:                        Object readerInfo = readDataIter.next();
106:                        String readerName = null;
107:                        if (readerInfo instanceof  String) {
108:                            readerName = (String) readerInfo;
109:                        } else if (readerInfo instanceof  Element) {
110:                            readerName = ((Element) readerInfo)
111:                                    .getAttribute("reader-name");
112:                        } else {
113:                            throw new IllegalArgumentException(
114:                                    "Reader name list does not contain String(s) or Element(s)");
115:                        }
116:
117:                        // get all of the main resource model stuff, ie specified in the entityengine.xml file
118:                        EntityDataReaderInfo entityDataReaderInfo = EntityConfigUtil
119:                                .getEntityDataReaderInfo(readerName);
120:
121:                        if (entityDataReaderInfo != null) {
122:                            List resourceElements = entityDataReaderInfo.resourceElements;
123:                            Iterator resIter = resourceElements.iterator();
124:                            while (resIter.hasNext()) {
125:                                Element resourceElement = (Element) resIter
126:                                        .next();
127:                                ResourceHandler handler = new MainResourceHandler(
128:                                        EntityConfigUtil.ENTITY_ENGINE_XML_FILENAME,
129:                                        resourceElement);
130:                                try {
131:                                    urlList.add(handler.getURL());
132:                                } catch (GenericConfigException e) {
133:                                    String errorMsg = "Could not get URL for Main ResourceHandler: "
134:                                            + e.toString();
135:                                    Debug.logWarning(errorMsg, module);
136:                                }
137:                            }
138:
139:                            // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file
140:                            List componentResourceInfos = ComponentConfig
141:                                    .getAllEntityResourceInfos("data",
142:                                            componentName);
143:                            Iterator componentResourceInfoIter = componentResourceInfos
144:                                    .iterator();
145:                            while (componentResourceInfoIter.hasNext()) {
146:                                ComponentConfig.EntityResourceInfo componentResourceInfo = (ComponentConfig.EntityResourceInfo) componentResourceInfoIter
147:                                        .next();
148:                                if (readerName
149:                                        .equals(componentResourceInfo.readerName)) {
150:                                    ResourceHandler handler = componentResourceInfo
151:                                            .createResourceHandler();
152:                                    try {
153:                                        urlList.add(handler.getURL());
154:                                    } catch (GenericConfigException e) {
155:                                        String errorMsg = "Could not get URL for Component ResourceHandler: "
156:                                                + e.toString();
157:                                        Debug.logWarning(errorMsg, module);
158:                                    }
159:                                }
160:                            }
161:                        } else {
162:                            String errorMsg = "Could not find entity-date-reader named: "
163:                                    + readerName;
164:                            Debug.logWarning(errorMsg, module);
165:                        }
166:                    }
167:                } else {
168:                    String errorMsg = "Could not find datasource named: "
169:                            + helperName;
170:                    Debug.logWarning(errorMsg, module);
171:                }
172:
173:                // get files from the paths string
174:                if (paths != null && paths.length() > 0) {
175:                    StringTokenizer tokenizer = new StringTokenizer(paths, ";");
176:                    while (tokenizer.hasMoreTokens()) {
177:                        String path = tokenizer.nextToken().toLowerCase();
178:                        File loadDir = new File(path);
179:                        if (loadDir.exists() && loadDir.isDirectory()) {
180:                            File[] files = loadDir.listFiles();
181:                            List tempFileList = new LinkedList();
182:                            for (int i = 0; i < files.length; i++) {
183:                                if (files[i].getName().toLowerCase().endsWith(
184:                                        ".xml")) {
185:                                    tempFileList.add(files[i]);
186:                                }
187:                            }
188:                            Collections.sort(tempFileList);
189:                            Iterator tempFileIter = tempFileList.iterator();
190:                            while (tempFileIter.hasNext()) {
191:                                File dataFile = (File) tempFileIter.next();
192:                                if (dataFile.exists()) {
193:                                    URL url = null;
194:                                    try {
195:                                        url = dataFile.toURI().toURL();
196:                                        urlList.add(url);
197:                                    } catch (java.net.MalformedURLException e) {
198:                                        String xmlError = "Error loading XML file \""
199:                                                + dataFile.getAbsolutePath()
200:                                                + "\"; Error was: "
201:                                                + e.getMessage();
202:                                        Debug.logError(xmlError, module);
203:                                    }
204:                                } else {
205:                                    String errorMsg = "Could not find file: \""
206:                                            + dataFile.getAbsolutePath() + "\"";
207:                                    Debug.logError(errorMsg, module);
208:                                }
209:                            }
210:                        }
211:                    }
212:                }
213:
214:                return urlList;
215:            }
216:
217:            public static int loadData(URL dataUrl, String helperName,
218:                    GenericDelegator delegator, List errorMessages)
219:                    throws GenericEntityException {
220:                return loadData(dataUrl, helperName, delegator, errorMessages,
221:                        -1);
222:            }
223:
224:            public static int loadData(URL dataUrl, String helperName,
225:                    GenericDelegator delegator, List errorMessages,
226:                    int txTimeout) throws GenericEntityException {
227:                return loadData(dataUrl, helperName, delegator, errorMessages,
228:                        txTimeout, false, false, false);
229:            }
230:
231:            public static int loadData(URL dataUrl, String helperName,
232:                    GenericDelegator delegator, List errorMessages,
233:                    int txTimeout, boolean dummyFks, boolean maintainTxs,
234:                    boolean tryInsert) throws GenericEntityException {
235:                int rowsChanged = 0;
236:
237:                if (dataUrl == null) {
238:                    String errMsg = "Cannot load data, dataUrl was null";
239:                    errorMessages.add(errMsg);
240:                    Debug.logError(errMsg, module);
241:                    return 0;
242:                }
243:
244:                Debug.logVerbose("[install.loadData] Loading XML Resource: \""
245:                        + dataUrl.toExternalForm() + "\"", module);
246:
247:                try {
248:                    /* The OLD way
249:                      List toBeStored = delegator.readXmlDocument(url);
250:                      delegator.storeAll(toBeStored);
251:                      rowsChanged += toBeStored.size();
252:                     */
253:
254:                    EntitySaxReader reader = null;
255:                    if (txTimeout > 0) {
256:                        reader = new EntitySaxReader(delegator, txTimeout);
257:                    } else {
258:                        reader = new EntitySaxReader(delegator);
259:                    }
260:                    reader.setCreateDummyFks(dummyFks);
261:                    reader.setMaintainTxStamps(maintainTxs);
262:                    rowsChanged += reader.parse(dataUrl);
263:                } catch (Exception e) {
264:                    String xmlError = "[install.loadData]: Error loading XML Resource \""
265:                            + dataUrl.toExternalForm()
266:                            + "\"; Error was: "
267:                            + e.getMessage();
268:                    errorMessages.add(xmlError);
269:                    Debug.logError(e, xmlError, module);
270:                }
271:
272:                return rowsChanged;
273:            }
274:
275:            public static int generateData(GenericDelegator delegator,
276:                    List errorMessages) throws GenericEntityException {
277:                int rowsChanged = 0;
278:                ModelReader reader = delegator.getModelReader();
279:                Collection entityCol = reader.getEntityNames();
280:                Iterator classNamesIterator = entityCol.iterator();
281:                while (classNamesIterator != null
282:                        && classNamesIterator.hasNext()) {
283:                    ModelEntity entity = reader
284:                            .getModelEntity((String) classNamesIterator.next());
285:                    String baseName = entity.getPlainTableName();
286:                    if (entity instanceof  ModelViewEntity) {
287:                        baseName = ModelUtil.javaNameToDbName(entity
288:                                .getEntityName());
289:                    }
290:
291:                    if (baseName != null) {
292:                        try {
293:                            List toBeStored = new LinkedList();
294:                            toBeStored.add(delegator.makeValue(
295:                                    "SecurityPermission", UtilMisc.toMap(
296:                                            "permissionId",
297:                                            baseName + "_ADMIN", "description",
298:                                            "Permission to Administer a "
299:                                                    + entity.getEntityName()
300:                                                    + " entity.")));
301:                            toBeStored.add(delegator.makeValue(
302:                                    "SecurityGroupPermission", UtilMisc
303:                                            .toMap("groupId", "FULLADMIN",
304:                                                    "permissionId", baseName
305:                                                            + "_ADMIN")));
306:                            rowsChanged += delegator.storeAll(toBeStored);
307:                        } catch (GenericEntityException e) {
308:                            errorMessages
309:                                    .add("[install.generateData] ERROR: Failed Security Generation for entity \""
310:                                            + baseName + "\"");
311:                        }
312:
313:                        /*
314:                        toStore.add(delegator.makeValue("SecurityPermission", UtilMisc.toMap("permissionId", baseName + "_VIEW", "description", "Permission to View a " + entity.getEntityName() + " entity.")));
315:                        toStore.add(delegator.makeValue("SecurityPermission", UtilMisc.toMap("permissionId", baseName + "_CREATE", "description", "Permission to Create a " + entity.getEntityName() + " entity.")));
316:                        toStore.add(delegator.makeValue("SecurityPermission", UtilMisc.toMap("permissionId", baseName + "_UPDATE", "description", "Permission to Update a " + entity.getEntityName() + " entity.")));
317:                        toStore.add(delegator.makeValue("SecurityPermission", UtilMisc.toMap("permissionId", baseName + "_DELETE", "description", "Permission to Delete a " + entity.getEntityName() + " entity.")));
318:                        
319:                        toStore.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FLEXADMIN", "permissionId", baseName + "_VIEW")));
320:                        toStore.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FLEXADMIN", "permissionId", baseName + "_CREATE")));
321:                        toStore.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FLEXADMIN", "permissionId", baseName + "_UPDATE")));
322:                        toStore.add(delegator.makeValue("SecurityGroupPermission", UtilMisc.toMap("groupId", "FLEXADMIN", "permissionId", baseName + "_DELETE")));
323:                         */
324:                    }
325:                }
326:
327:                return rowsChanged;
328:            }
329:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.