📄 webtoolsservices.java
字号:
if (txTimeout == null) { txTimeout = new Integer(7200); } if (filePause == null) { filePause = new Long(0); } if (path != null && path.length() > 0) { long pauseLong = filePause != null ? filePause.longValue() : 0; File baseDir = new File(path); if (baseDir.isDirectory() && baseDir.canRead()) { File[] fileArray = baseDir.listFiles(); ArrayList files = new ArrayList(fileArray.length); for (int a=0; a<fileArray.length; a++){ if (fileArray[a].getName().toUpperCase().endsWith("XML")) { files.add(fileArray[a]); } } boolean importedOne = false; int fileListMarkedSize = files.size(); int passes = 0; for (int a=0; a<files.size(); a++){ // Infinite loop defense if (a == fileListMarkedSize) { passes++; fileListMarkedSize = files.size(); messages.add("Pass " + passes + " complete"); // This means we've done a pass if ( false == importedOne ) { // We've failed to make any imports messages.add("Dropping out as we failed to make any imports on the last pass"); a = files.size(); continue; } importedOne = false; } File curFile = (File)files.get(a); try{ URL url = curFile.toURL(); Map inputMap = UtilMisc.toMap("url", url, "mostlyInserts", mostlyInserts, "createDummyFks", createDummyFks, "maintainTimeStamps", maintainTimeStamps, "txTimeout", txTimeout, "userLogin", userLogin); Map outputMap = dispatcher.runSync("parseEntityXmlFile", inputMap); Long numberRead = (Long)outputMap.get("rowProcessed"); messages.add("Got " + numberRead.longValue() + " entities from " + curFile); importedOne = true; if (deleteFiles) { curFile.delete(); } } catch (Exception ex){ messages.add("Error trying to read from " + curFile + ": " + ex); if (ex.toString().indexOf("referential integrity violation") > -1 || ex.toString().indexOf("Integrity constraint violation") > -1){ //It didn't work because object it depends on are still //missing from the DB. Retry later. // //FIXME: Of course this is a potential infinite loop. messages.add("Looks like referential integrity violation, will retry"); files.add(curFile); } } // pause in between files if (pauseLong > 0) { Debug.log("Pausing for [" + pauseLong + "] seconds - " + UtilDateTime.nowTimestamp()); try { Thread.sleep((pauseLong * 1000)); } catch(InterruptedException ie) { Debug.log("Pause finished - " + UtilDateTime.nowTimestamp()); } } } } else { messages.add("path not found or can't be read"); } } else { messages.add("No path specified, doing nothing."); } // send the notification Map resp = UtilMisc.toMap("messages", messages); return resp; } public static Map parseEntityXmlFile(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); URL url = (URL)context.get("url"); String xmltext = (String)context.get("xmltext"); if (url == null && xmltext == null) { return ServiceUtil.returnError("No entity xml file or text specified"); } boolean mostlyInserts = (String)context.get("mostlyInserts") != null; boolean maintainTimeStamps = (String)context.get("maintainTimeStamps") != null; boolean createDummyFks = (String)context.get("createDummyFks") != null; Integer txTimeout = (Integer)context.get("txTimeout"); if (txTimeout == null) { txTimeout = new Integer(7200); } Long rowProcessed = new Long(0); try { EntitySaxReader reader = new EntitySaxReader(delegator); reader.setUseTryInsertMethod(mostlyInserts); reader.setMaintainTxStamps(maintainTimeStamps); reader.setTransactionTimeout(txTimeout.intValue()); reader.setCreateDummyFks(createDummyFks); long numberRead = (url != null? reader.parse(url): reader.parse(xmltext)); rowProcessed = new Long(numberRead); } catch (Exception ex){ return ServiceUtil.returnError("Error parsing entity xml file: " + ex.toString()); } // send the notification Map resp = UtilMisc.toMap("rowProcessed", rowProcessed); return resp; } public static Map entityExportAll(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String outpath = (String)context.get("outpath"); // mandatory Integer txTimeout = (Integer)context.get("txTimeout"); if (txTimeout == null) { txTimeout = new Integer(7200); } List results = new ArrayList(); if (outpath != null && outpath.length() > 0) { File outdir = new File(outpath); if (!outdir.exists()) { outdir.mkdir(); } if (outdir.isDirectory() && outdir.canWrite()) { Iterator passedEntityNames = null; try { ModelReader reader = delegator.getModelReader(); Collection ec = reader.getEntityNames(); TreeSet entityNames = new TreeSet(ec); passedEntityNames = entityNames.iterator(); } catch(Exception exc) { return ServiceUtil.returnError("Error retrieving entity names."); } int fileNumber = 1; while (passedEntityNames.hasNext()) { long numberWritten = 0; String curEntityName = (String)passedEntityNames.next(); EntityListIterator values = null; try { ModelEntity me = delegator.getModelEntity(curEntityName); if (me instanceof ModelViewEntity) { results.add("["+fileNumber +"] [vvv] " + curEntityName + " skipping view entity"); continue; } // some databases don't support cursors, or other problems may happen, so if there is an error here log it and move on to get as much as possible try { values = delegator.findListIteratorByCondition(curEntityName, null, null, null, me.getPkFieldNames(), null); } catch (Exception entityEx) { results.add("["+fileNumber +"] [xxx] Error when writing " + curEntityName + ": " + entityEx); continue; } //Don't bother writing the file if there's nothing //to put into it GenericValue value = (GenericValue) values.next(); if (value != null) { PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outdir, curEntityName +".xml")), "UTF-8"))); writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writer.println("<entity-engine-xml>"); do { value.writeXmlText(writer, ""); numberWritten++; } while ((value = (GenericValue) values.next()) != null); writer.println("</entity-engine-xml>"); writer.close(); results.add("["+fileNumber +"] [" + numberWritten + "] " + curEntityName + " wrote " + numberWritten + " records"); } else { results.add("["+fileNumber +"] [---] " + curEntityName + " has no records, not writing file"); } values.close(); } catch (Exception ex) { if (values != null) { try { values.close(); } catch(Exception exc) { //Debug.warning(); } } results.add("["+fileNumber +"] [xxx] Error when writing " + curEntityName + ": " + ex); } fileNumber++; } } else { results.add("Path not found or no write access."); } } else { results.add("No path specified, doing nothing."); } // send the notification Map resp = UtilMisc.toMap("results", results); return resp; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -