Source Code Cross Referenced for EntityDataServices.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » entityext » data » 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 » SourceTap CRM » org.ofbiz.entityext.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: EntityDataServices.java,v 1.12 2004/02/06 19:46:34 ajzeneski Exp $
003:         *
004:         * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005:         *
006:         * Permission is hereby granted, free of charge, to any person obtaining a
007:         * copy of this software and associated documentation files (the "Software"),
008:         * to deal in the Software without restriction, including without limitation
009:         * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         * and/or sell copies of the Software, and to permit persons to whom the
011:         * Software is furnished to do so, subject to the following conditions:
012:         *
013:         * The above copyright notice and this permission notice shall be included
014:         * in all copies or substantial portions of the Software.
015:         *
016:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         *
024:         */
025:        package org.ofbiz.entityext.data;
026:
027:        import org.ofbiz.service.ServiceUtil;
028:        import org.ofbiz.service.DispatchContext;
029:        import org.ofbiz.service.LocalDispatcher;
030:        import org.ofbiz.service.GenericServiceException;
031:        import org.ofbiz.security.Security;
032:        import org.ofbiz.entity.GenericDelegator;
033:        import org.ofbiz.entity.GenericValue;
034:        import org.ofbiz.entity.model.ModelEntity;
035:        import org.ofbiz.entity.model.ModelField;
036:        import org.ofbiz.base.util.GeneralException;
037:        import org.ofbiz.base.util.Debug;
038:        import org.ofbiz.base.util.UtilURL;
039:        import org.ofbiz.base.util.UtilMisc;
040:
041:        import java.util.*;
042:        import java.io.*;
043:        import java.net.URI;
044:        import java.net.URL;
045:        import java.net.URISyntaxException;
046:
047:        /**
048:         * Entity Data Import/Export Services
049:         *
050:         * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
051:         * @version    $Revision: 1.12 $
052:         * @since      2.1
053:         */
054:        public class EntityDataServices {
055:
056:            public static final String module = EntityDataServices.class
057:                    .getName();
058:
059:            public static Map exportDelimitedToDirectory(DispatchContext dctx,
060:                    Map context) {
061:                return ServiceUtil
062:                        .returnError("This service is not implemented yet.");
063:            }
064:
065:            public static Map importDelimitedFromDirectory(
066:                    DispatchContext dctx, Map context) {
067:                LocalDispatcher dispatcher = dctx.getDispatcher();
068:                GenericDelegator delegator = dctx.getDelegator();
069:                Security security = dctx.getSecurity();
070:
071:                // check permission
072:                GenericValue userLogin = (GenericValue) context
073:                        .get("userLogin");
074:                if (!security.hasPermission("ENTITY_MAINT", userLogin)) {
075:                    return ServiceUtil
076:                            .returnError("You do not have permission to run this service.");
077:                }
078:
079:                // get the directory & delimiter
080:                String rootDirectory = (String) context.get("rootDirectory");
081:                URL rootDirectoryUrl = UtilURL.fromResource(rootDirectory);
082:                if (rootDirectoryUrl == null) {
083:                    return ServiceUtil
084:                            .returnError("Unable to locate root directory : "
085:                                    + rootDirectory);
086:                }
087:
088:                String delimiter = (String) context.get("delimiter");
089:                if (delimiter == null) {
090:                    // default delimiter is tab
091:                    delimiter = "\t";
092:                }
093:
094:                File root = null;
095:                try {
096:                    root = new File(new URI(rootDirectoryUrl.toExternalForm()));
097:                } catch (URISyntaxException e) {
098:                    return ServiceUtil
099:                            .returnError("Unable to get root directory URI");
100:                }
101:
102:                if (!root.exists() || !root.isDirectory() || !root.canRead()) {
103:                    return ServiceUtil
104:                            .returnError("Root directory does not exist or is not readable.");
105:                }
106:
107:                // get the file list
108:                List files = getFileList(root);
109:                if (files != null && files.size() > 0) {
110:                    Iterator i = files.iterator();
111:                    while (i.hasNext()) {
112:                        File file = (File) i.next();
113:                        try {
114:                            Map serviceCtx = UtilMisc.toMap("file", file,
115:                                    "delimiter", delimiter, "userLogin",
116:                                    userLogin);
117:                            dispatcher.runSyncIgnore(
118:                                    "importDelimitedEntityFile", serviceCtx);
119:                        } catch (GenericServiceException e) {
120:                            Debug.logError(e, module);
121:                        }
122:                    }
123:                } else {
124:                    return ServiceUtil
125:                            .returnError("No files available for reading in this root directory : "
126:                                    + rootDirectory);
127:                }
128:
129:                return ServiceUtil.returnSuccess();
130:            }
131:
132:            public static Map importDelimitedFile(DispatchContext dctx,
133:                    Map context) {
134:                GenericDelegator delegator = dctx.getDelegator();
135:                Security security = dctx.getSecurity();
136:
137:                // check permission
138:                GenericValue userLogin = (GenericValue) context
139:                        .get("userLogin");
140:                if (!security.hasPermission("ENTITY_MAINT", userLogin)) {
141:                    return ServiceUtil
142:                            .returnError("You do not have permission to run this service.");
143:                }
144:
145:                String delimiter = (String) context.get("delimiter");
146:                if (delimiter == null) {
147:                    // default delimiter is tab
148:                    delimiter = "\t";
149:                }
150:
151:                long startTime = System.currentTimeMillis();
152:
153:                File file = (File) context.get("file");
154:                int records = 0;
155:                try {
156:                    records = readEntityFile(file, delimiter, delegator);
157:                } catch (GeneralException e) {
158:                    return ServiceUtil.returnError(e.getMessage());
159:                } catch (FileNotFoundException e) {
160:                    return ServiceUtil.returnError("File not found : "
161:                            + file.getName());
162:                } catch (IOException e) {
163:                    Debug.logError(e, module);
164:                    return ServiceUtil.returnError("Problem reading file : "
165:                            + file.getName());
166:                }
167:
168:                long endTime = System.currentTimeMillis();
169:                long runTime = endTime - startTime;
170:
171:                Debug.logInfo("Imported/Updated [" + records + "] from : "
172:                        + file.getAbsolutePath() + " [" + runTime + "ms]",
173:                        module);
174:                Map result = ServiceUtil.returnSuccess();
175:                result.put("records", new Integer(records));
176:                return result;
177:            }
178:
179:            private static List getFileList(File root) {
180:                List fileList = new ArrayList();
181:
182:                // check for a file list file
183:                File listFile = new File(root, "FILELIST.txt");
184:                Debug.logInfo("Checking file list - " + listFile.getPath(),
185:                        module);
186:                if (listFile.exists()) {
187:                    BufferedReader reader = null;
188:                    try {
189:                        reader = new BufferedReader(new FileReader(listFile));
190:                    } catch (FileNotFoundException e) {
191:                        Debug.logError(e, module);
192:                    }
193:                    if (reader != null) {
194:                        // read each line as a file name to load
195:                        String line;
196:                        try {
197:                            while ((line = reader.readLine()) != null) {
198:                                line = line.trim();
199:                                File this File = new File(root, line);
200:                                if (this File.exists()) {
201:                                    fileList.add(this File);
202:                                }
203:                            }
204:                        } catch (IOException e) {
205:                            Debug.logError(e, module);
206:                        }
207:
208:                        // close the reader
209:                        try {
210:                            reader.close();
211:                        } catch (IOException e) {
212:                            Debug.logError(e, module);
213:                        }
214:                        Debug.logInfo("Read file list : " + fileList.size()
215:                                + " entities.", module);
216:                    }
217:                } else {
218:                    File[] files = root.listFiles();
219:                    for (int i = 0; i < files.length; i++) {
220:                        String fileName = files[i].getName();
221:                        if (!fileName.startsWith("_")
222:                                && fileName.endsWith(".txt")) {
223:                            fileList.add(files[i]);
224:                        }
225:                    }
226:                    Debug.logInfo(
227:                            "No file list found; using directory order : "
228:                                    + fileList.size() + " entities.", module);
229:                }
230:
231:                return fileList;
232:            }
233:
234:            private static String[] readEntityHeader(File file,
235:                    String delimiter, BufferedReader dataReader)
236:                    throws IOException {
237:                String filePath = file.getPath().replace('\\', '/');
238:
239:                String[] header = null;
240:                File headerFile = new File(filePath.substring(0, filePath
241:                        .lastIndexOf('/')), "_" + file.getName());
242:
243:                boolean uniqueHeaderFile = true;
244:                BufferedReader reader = null;
245:                if (headerFile.exists()) {
246:                    reader = new BufferedReader(new FileReader(headerFile));
247:                } else {
248:                    uniqueHeaderFile = false;
249:                    reader = dataReader;
250:                }
251:
252:                // read one line from either the header file or the data file if no header file exists
253:                String firstLine = reader.readLine();
254:                if (firstLine != null) {
255:                    header = firstLine.split(delimiter);
256:                }
257:
258:                if (uniqueHeaderFile) {
259:                    reader.close();
260:                }
261:
262:                return header;
263:            }
264:
265:            private static int readEntityFile(File file, String delimiter,
266:                    GenericDelegator delegator) throws IOException,
267:                    GeneralException {
268:                String entityName = file.getName().substring(0,
269:                        file.getName().lastIndexOf('.'));
270:                if (entityName == null) {
271:                    throw new GeneralException("Entity name cannot be null : ["
272:                            + file.getName() + "]");
273:                }
274:
275:                BufferedReader reader = new BufferedReader(new FileReader(file));
276:                String[] header = readEntityHeader(file, delimiter, reader);
277:
278:                //Debug.log("Opened data file [" + file.getName() + "] now running...", module);
279:                GeneralException exception = null;
280:                String line = null;
281:                int lineNumber = 1;
282:                while ((line = reader.readLine()) != null) {
283:                    // process the record
284:                    String fields[] = line.split(delimiter);
285:                    //Debug.log("Split record", module);
286:                    if (fields.length < 1) {
287:                        exception = new GeneralException(
288:                                "Illegal number of fields [" + file.getName()
289:                                        + " / " + lineNumber);
290:                        break;
291:                    }
292:
293:                    GenericValue newValue = makeGenericValue(delegator,
294:                            entityName, header, fields);
295:                    //Debug.log("Made value object", module);
296:                    newValue = delegator.createOrStore(newValue);
297:                    //Debug.log("Stored record", module);
298:
299:                    if (lineNumber % 500 == 0 || lineNumber == 1) {
300:                        Debug.log("Records Stored [" + file.getName() + "]: "
301:                                + lineNumber, module);
302:                        //Debug.log("Last record : " + newValue, module);
303:                    }
304:
305:                    lineNumber++;
306:                }
307:                reader.close();
308:
309:                // now that we closed the reader; throw the exception
310:                if (exception != null) {
311:                    throw exception;
312:                }
313:
314:                return lineNumber;
315:            }
316:
317:            private static GenericValue makeGenericValue(
318:                    GenericDelegator delegator, String entityName,
319:                    String[] header, String[] line) {
320:                GenericValue newValue = delegator.makeValue(entityName, null);
321:                for (int i = 0; i < header.length; i++) {
322:                    String name = header[i].trim();
323:
324:                    String value = null;
325:                    if (i < line.length) {
326:                        value = line[i];
327:                    }
328:
329:                    // check for null values
330:                    if (value != null && value.length() > 0) {
331:                        char first = value.charAt(0);
332:                        if (first == 0x00) {
333:                            value = null;
334:                        }
335:
336:                        // trim non-null values
337:                        if (value != null) {
338:                            value = value.trim();
339:                        }
340:
341:                        if (value != null && value.length() == 0) {
342:                            value = null;
343:                        }
344:                    } else {
345:                        value = null;
346:                    }
347:
348:                    // convert and set the fields
349:                    newValue.setString(name, value);
350:                }
351:                return newValue;
352:            }
353:
354:            private String[] getEntityFieldNames(GenericDelegator delegator,
355:                    String entityName) {
356:                ModelEntity entity = delegator.getModelEntity(entityName);
357:                if (entity == null) {
358:                    return null;
359:                }
360:                List modelFields = entity.getFieldsCopy();
361:                if (modelFields == null) {
362:                    return null;
363:                }
364:
365:                String[] fieldNames = new String[modelFields.size()];
366:                for (int i = 0; i < modelFields.size(); i++) {
367:                    ModelField field = (ModelField) modelFields.get(i);
368:                    fieldNames[i] = field.getName();
369:                }
370:
371:                return fieldNames;
372:            }
373:
374:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.