📄 webtoolsservices.java
字号:
/* * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.webtools;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.ArrayList;import java.util.Collection;import java.util.TreeSet;import java.util.Locale;import java.util.Map;import java.io.File;import java.io.InputStream;import java.io.StringReader;import java.io.StringWriter;import java.io.FileReader;import java.io.PrintWriter;import java.io.BufferedWriter;import java.io.OutputStreamWriter;import java.io.FileOutputStream;import java.io.IOException;import java.io.FileNotFoundException;import java.net.URL;import java.net.MalformedURLException;import javolution.util.FastMap;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilURL;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityListIterator;import org.ofbiz.entity.util.EntitySaxReader;import org.ofbiz.entity.model.ModelReader;import org.ofbiz.entity.model.ModelEntity;import org.ofbiz.entity.model.ModelViewEntity;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.xml.sax.InputSource;import org.w3c.dom.*;import freemarker.template.*;import freemarker.ext.dom.NodeModel;import freemarker.ext.beans.BeansWrapper;/** * WebTools Services * * @author <a href="mailto:tiz@sastau.it">Jacopo Cappellato</a> * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> */public class WebToolsServices { public static final String module = WebToolsServices.class.getName(); public static Map entityImport(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); List messages = new ArrayList(); String filename = (String)context.get("filename"); String fmfilename = (String)context.get("fmfilename"); String fulltext = (String)context.get("fulltext"); boolean isUrl = (String)context.get("isUrl") != null; String mostlyInserts = (String)context.get("mostlyInserts"); String maintainTimeStamps = (String)context.get("maintainTimeStamps"); String createDummyFks = (String)context.get("createDummyFks"); Integer txTimeout = (Integer)context.get("txTimeout"); if (txTimeout == null) { txTimeout = new Integer(7200); } InputSource ins = null; URL url = null; // ############################# // The filename to parse is prepared // ############################# if (filename != null && filename.length() > 0) { try { url = isUrl?new URL(filename):UtilURL.fromFilename(filename); InputStream is = url.openStream(); ins = new InputSource(is); } catch(MalformedURLException mue) { return ServiceUtil.returnError("ERROR: invalid file name (" + filename + "): " + mue.getMessage()); } catch(IOException ioe) { return ServiceUtil.returnError("ERROR reading file name (" + filename + "): " + ioe.getMessage()); } catch(Exception exc) { return ServiceUtil.returnError("ERROR: reading file name (" + filename + "): " + exc.getMessage()); } } // ############################# // The text to parse is prepared // ############################# if (fulltext != null && fulltext.length() > 0) { StringReader sr = new StringReader(fulltext); ins = new InputSource(sr); } // ############################# // FM Template // ############################# String s = null; if (UtilValidate.isNotEmpty(fmfilename) && ins != null) { FileReader templateReader = null; try { templateReader = new FileReader(fmfilename); } catch(FileNotFoundException e) { return ServiceUtil.returnError("ERROR reading template file (" + fmfilename + "): " + e.getMessage()); } StringWriter outWriter = new StringWriter(); Template template = null; try { Configuration conf = org.ofbiz.base.util.template.FreeMarkerWorker.makeDefaultOfbizConfig(); template = new Template("FMImportFilter", templateReader, conf); Map fmcontext = new HashMap(); NodeModel nodeModel = NodeModel.parse(ins); fmcontext.put("doc", nodeModel); BeansWrapper wrapper = BeansWrapper.getDefaultInstance(); TemplateHashModel staticModels = wrapper.getStaticModels(); fmcontext.put("Static", staticModels); template.process(fmcontext, outWriter); s = outWriter.toString(); } catch(Exception ex) { return ServiceUtil.returnError("ERROR processing template file (" + fmfilename + "): " + ex.getMessage()); } } // ############################# // The parsing takes place // ############################# if (s != null || fulltext != null || url != null) { try{ Map inputMap = UtilMisc.toMap("mostlyInserts", mostlyInserts, "createDummyFks", createDummyFks, "maintainTimeStamps", maintainTimeStamps, "txTimeout", txTimeout, "userLogin", userLogin); if (s != null) { inputMap.put("xmltext", s); } else { if (fulltext != null) { inputMap.put("xmltext", fulltext); } else { inputMap.put("url", url); } } Map outputMap = dispatcher.runSync("parseEntityXmlFile", inputMap); if (ServiceUtil.isError(outputMap)) { return ServiceUtil.returnError("ERROR: " + ServiceUtil.getErrorMessage(outputMap)); } else { Long numberRead = (Long)outputMap.get("rowProcessed"); messages.add("Got " + numberRead.longValue() + " entities to write to the datasource."); } } catch (Exception ex){ return ServiceUtil.returnError("ERROR parsing Entity Xml file: " + ex.getMessage()); } } else { messages.add("No filename/URL or complete XML document specified, doing nothing."); } // send the notification Map resp = UtilMisc.toMap("messages", messages); return resp; } public static Map entityImportDir(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); List messages = new ArrayList(); String path = (String)context.get("path"); String mostlyInserts = (String)context.get("mostlyInserts"); String maintainTimeStamps = (String)context.get("maintainTimeStamps"); String createDummyFks = (String)context.get("createDummyFks"); boolean deleteFiles = (String)context.get("deleteFiles") != null; Integer txTimeout = (Integer)context.get("txTimeout"); Long filePause = (Long)context.get("filePause");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -