Search in sources :

Example 36 with GenericServiceException

use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.

the class EmailServices method sendFailureNotification.

public static void sendFailureNotification(DispatchContext dctx, Map<String, ? extends Object> context, MimeMessage message, List<SMTPAddressFailedException> failures) {
    Locale locale = (Locale) context.get("locale");
    Map<String, Object> newContext = new LinkedHashMap<>();
    newContext.put("userLogin", context.get("userLogin"));
    newContext.put("sendFailureNotification", false);
    newContext.put("sendFrom", context.get("sendFrom"));
    newContext.put("sendTo", context.get("sendFrom"));
    newContext.put("subject", UtilProperties.getMessage(resource, "CommonEmailSendUndeliveredMail", locale));
    StringBuilder sb = new StringBuilder();
    sb.append(UtilProperties.getMessage(resource, "CommonEmailDeliveryFailed", locale));
    sb.append("/n/n");
    for (SMTPAddressFailedException failure : failures) {
        sb.append(failure.getAddress());
        sb.append(": ");
        sb.append(failure.getMessage());
        sb.append("/n/n");
    }
    sb.append(UtilProperties.getMessage(resource, "CommonEmailDeliveryOriginalMessage", locale));
    sb.append("/n/n");
    List<Map<String, Object>> bodyParts = new LinkedList<>();
    bodyParts.add(UtilMisc.<String, Object>toMap("content", sb.toString(), "type", "text/plain"));
    try {
        bodyParts.add(UtilMisc.<String, Object>toMap("content", message.getDataHandler()));
    } catch (MessagingException e) {
        Debug.logError(e, module);
    }
    newContext.put("bodyParts", bodyParts);
    try {
        dctx.getDispatcher().runSync("sendMailMultiPart", newContext);
    } catch (GenericServiceException e) {
        Debug.logError(e, module);
    }
}
Also used : Locale(java.util.Locale) MessagingException(javax.mail.MessagingException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap)

Example 37 with GenericServiceException

use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.

the class QRCodeEvents method serveQRCodeImage.

/**
 * Streams QR Code to the output.
 */
public static String serveQRCodeImage(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
    Map<String, Object> parameters = UtilHttp.getParameterMap(request);
    String message = (String) parameters.get("message");
    GenericValue userLogin = (GenericValue) request.getAttribute("userLogin");
    if (userLogin == null) {
        userLogin = (GenericValue) session.getAttribute("userLogin");
    }
    if (userLogin == null) {
        userLogin = (GenericValue) session.getAttribute("autoUserLogin");
    }
    Locale locale = UtilHttp.getLocale(request);
    if (UtilValidate.isEmpty(message)) {
        message = "Error get message parameter.";
    }
    String format = (String) parameters.get("format");
    if (UtilValidate.isEmpty(format)) {
        format = "jpg";
    }
    String mimeType = "image/" + format;
    String width = (String) parameters.get("width");
    String height = (String) parameters.get("height");
    String encoding = (String) parameters.get("encoding");
    Boolean verifyOutput = Boolean.valueOf((String) parameters.get("verifyOutput"));
    String logoImageMaxWidth = (String) parameters.get("logoImageMaxWidth");
    String logoImageMaxHeight = (String) parameters.get("logoImageMaxHeight");
    try {
        response.setContentType(mimeType);
        OutputStream os = response.getOutputStream();
        Map<String, Object> context = UtilMisc.<String, Object>toMap("message", message, "format", format, "userLogin", userLogin, "locale", locale);
        if (UtilValidate.isNotEmpty(width)) {
            try {
                context.put("width", Integer.parseInt(width));
            } catch (NumberFormatException e) {
                Debug.logWarning(e, e.getMessage(), module);
            }
            if (UtilValidate.isEmpty(height)) {
                try {
                    context.put("height", Integer.parseInt(width));
                } catch (NumberFormatException e) {
                    Debug.logWarning(e, e.getMessage(), module);
                }
            }
        }
        if (UtilValidate.isNotEmpty(height)) {
            try {
                context.put("height", Integer.parseInt(height));
            } catch (NumberFormatException e) {
                Debug.logWarning(e, e.getMessage(), module);
            }
            if (UtilValidate.isEmpty(width)) {
                try {
                    context.put("width", Integer.parseInt(height));
                } catch (NumberFormatException e) {
                    Debug.logWarning(e, e.getMessage(), module);
                }
            }
        }
        if (UtilValidate.isNotEmpty(encoding)) {
            context.put("encoding", encoding);
        }
        if (UtilValidate.isNotEmpty(verifyOutput) && verifyOutput.booleanValue()) {
            context.put("verifyOutput", verifyOutput);
        }
        if (UtilValidate.isNotEmpty(logoImageMaxWidth)) {
            try {
                context.put("logoImageMaxWidth", Integer.parseInt(logoImageMaxWidth));
            } catch (NumberFormatException e) {
                Debug.logWarning(e, e.getMessage(), module);
            }
        }
        if (UtilValidate.isNotEmpty(logoImageMaxHeight)) {
            try {
                context.put("logoImageMaxHeight", Integer.parseInt(logoImageMaxHeight));
            } catch (NumberFormatException e) {
                Debug.logWarning(e, e.getMessage(), module);
            }
        }
        Map<String, Object> results = dispatcher.runSync("generateQRCodeImage", context);
        if (ServiceUtil.isSuccess(results)) {
            BufferedImage bufferedImage = (BufferedImage) results.get("bufferedImage");
            if (!ImageIO.write(bufferedImage, format, os)) {
                String errMsg = UtilProperties.getMessage("QRCodeUiLabels", "ErrorWriteFormatToFile", new Object[] { format }, locale);
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            os.flush();
        } else {
            String errMsg = ServiceUtil.getErrorMessage(results);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            return "error";
        }
    } catch (IOException | GenericServiceException e) {
        String errMsg = UtilProperties.getMessage("QRCodeUiLabels", "ErrorGenerateQRCode", new Object[] { e.getMessage() }, locale);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) HttpSession(javax.servlet.http.HttpSession) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 38 with GenericServiceException

use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.

the class ScriptHelperImpl method runService.

public Map<String, ? extends Object> runService(String serviceName, Map<String, ? extends Object> inputMap, Map<String, ? extends Object> args) throws ScriptException {
    Assert.notNull("serviceName", serviceName, "args", args);
    boolean includeUserLogin = !"false".equals(args.get("includeUserLoginStr"));
    String requireNewTransactionStr = (String) args.get("requireNewTransaction");
    int transactionTimeout = -1;
    if (UtilValidate.isNotEmpty(requireNewTransactionStr)) {
        String timeoutStr = (String) args.get("transactionTimout");
        if (UtilValidate.isNotEmpty(timeoutStr)) {
            try {
                transactionTimeout = Integer.parseInt(timeoutStr);
            } catch (NumberFormatException e) {
                Debug.logWarning(e, "Setting timeout to 0 (default)", module);
                transactionTimeout = 0;
            }
        }
    }
    Map<String, Object> inMap = new HashMap<String, Object>(inputMap);
    if (includeUserLogin && !inMap.containsKey("userLogin")) {
        GenericValue userLogin = ctxHelper.getUserLogin();
        if (userLogin != null) {
            inMap.put("userLogin", userLogin);
        }
    }
    if (!inMap.containsKey("locale") && ctxHelper.getLocale() != null) {
        inMap.put("locale", ctxHelper.getLocale());
    }
    if (!inMap.containsKey("timeZone") && ctxHelper.getTimeZone() != null) {
        inMap.put("timeZone", ctxHelper.getTimeZone());
    }
    Map<String, Object> result = null;
    try {
        if (UtilValidate.isEmpty(requireNewTransactionStr) && transactionTimeout < 0) {
            result = ctxHelper.getDispatcher().runSync(serviceName, inMap);
        } else {
            ModelService modelService = ctxHelper.getDispatcher().getDispatchContext().getModelService(serviceName);
            boolean requireNewTransaction = modelService.requireNewTransaction;
            int timeout = modelService.transactionTimeout;
            if (UtilValidate.isNotEmpty(requireNewTransactionStr)) {
                requireNewTransaction = "true".equals(requireNewTransactionStr);
            }
            if (transactionTimeout >= 0) {
                timeout = transactionTimeout;
            }
            result = ctxHelper.getDispatcher().runSync(serviceName, inMap, timeout, requireNewTransaction);
        }
    } catch (GenericServiceException e) {
        String errMsg = "Error running script " + ctxHelper.getScriptName() + " [problem invoking the [" + serviceName + "] service: " + e.getMessage();
        Debug.logWarning(e, errMsg, module);
        throw new ScriptException(errMsg);
    }
    return result;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ScriptException(javax.script.ScriptException) HashMap(java.util.HashMap) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ModelService(org.apache.ofbiz.service.ModelService)

Example 39 with GenericServiceException

use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.

the class EntitySyncServices method loadOfflineSyncData.

public static Map<String, Object> loadOfflineSyncData(DispatchContext dctx, Map<String, ? extends Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    String fileName = (String) context.get("xmlFileName");
    Locale locale = (Locale) context.get("locale");
    URL xmlFile = UtilURL.fromResource(fileName);
    if (xmlFile != null) {
        Document xmlSyncDoc = null;
        try {
            xmlSyncDoc = UtilXml.readXmlDocument(xmlFile, false);
        } catch (SAXException e) {
            Debug.logError(e, module);
        } catch (ParserConfigurationException e) {
            Debug.logError(e, module);
        } catch (IOException e) {
            Debug.logError(e, module);
        }
        if (xmlSyncDoc == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtEntitySyncXMLDocumentIsNotValid", UtilMisc.toMap("fileName", fileName), locale));
        }
        List<? extends Element> syncElements = UtilXml.childElementList(xmlSyncDoc.getDocumentElement());
        if (syncElements != null) {
            for (Element entitySync : syncElements) {
                String entitySyncId = entitySync.getAttribute("entitySyncId");
                String startTime = entitySync.getAttribute("lastSuccessfulSynchTime");
                String createString = UtilXml.childElementValue(entitySync, "values-to-create");
                String storeString = UtilXml.childElementValue(entitySync, "values-to-store");
                String removeString = UtilXml.childElementValue(entitySync, "keys-to-remove");
                // de-serialize the value lists
                try {
                    List<GenericValue> valuesToCreate = checkList(XmlSerializer.deserialize(createString, delegator), GenericValue.class);
                    List<GenericValue> valuesToStore = checkList(XmlSerializer.deserialize(storeString, delegator), GenericValue.class);
                    List<GenericEntity> keysToRemove = checkList(XmlSerializer.deserialize(removeString, delegator), GenericEntity.class);
                    Map<String, Object> storeContext = UtilMisc.toMap("entitySyncId", entitySyncId, "valuesToCreate", valuesToCreate, "valuesToStore", valuesToStore, "keysToRemove", keysToRemove, "userLogin", userLogin);
                    // store the value(s)
                    Map<String, Object> storeResult = dispatcher.runSync("storeEntitySyncData", storeContext);
                    if (ServiceUtil.isError(storeResult)) {
                        throw new Exception(ServiceUtil.getErrorMessage(storeResult));
                    }
                // TODO create a response document to send back to the initial sync machine
                } catch (GenericServiceException gse) {
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtUnableToLoadXMLDocument", UtilMisc.toMap("entitySyncId", entitySyncId, "startTime", startTime, "errorString", gse.getMessage()), locale));
                } catch (Exception e) {
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtUnableToLoadXMLDocument", UtilMisc.toMap("entitySyncId", entitySyncId, "startTime", startTime, "errorString", e.getMessage()), locale));
                }
            }
        }
    } else {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtOfflineXMLFileNotFound", UtilMisc.toMap("fileName", fileName), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) UtilURL(org.apache.ofbiz.base.util.UtilURL) SyncAbortException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) SyncErrorException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncErrorException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntity(org.apache.ofbiz.entity.GenericEntity) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 40 with GenericServiceException

use of org.apache.ofbiz.service.GenericServiceException in project ofbiz-framework by apache.

the class EntityDataServices method importDelimitedFromDirectory.

public static Map<String, Object> importDelimitedFromDirectory(DispatchContext dctx, Map<String, Object> context) {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Security security = dctx.getSecurity();
    Locale locale = (Locale) context.get("locale");
    // check permission
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    if (!security.hasPermission("ENTITY_MAINT", userLogin)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtServicePermissionNotGranted", locale));
    }
    // get the directory & delimiter
    String rootDirectory = (String) context.get("rootDirectory");
    URL rootDirectoryUrl = UtilURL.fromResource(rootDirectory);
    if (rootDirectoryUrl == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtUnableToLocateRootDirectory", UtilMisc.toMap("rootDirectory", rootDirectory), locale));
    }
    String delimiter = (String) context.get("delimiter");
    if (delimiter == null) {
        // default delimiter is tab
        delimiter = "\t";
    }
    File root = null;
    try {
        root = new File(new URI(rootDirectoryUrl.toExternalForm()));
    } catch (URISyntaxException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtUnableToLocateRootDirectoryURI", locale));
    }
    if (!root.exists() || !root.isDirectory() || !root.canRead()) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtRootDirectoryDoesNotExists", locale));
    }
    // get the file list
    List<File> files = getFileList(root);
    if (UtilValidate.isNotEmpty(files)) {
        for (File file : files) {
            try {
                Map<String, Object> serviceCtx = UtilMisc.toMap("file", file, "delimiter", delimiter, "userLogin", userLogin);
                dispatcher.runSyncIgnore("importDelimitedEntityFile", serviceCtx);
            } catch (GenericServiceException e) {
                Debug.logError(e, module);
            }
        }
    } else {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtNoFileAvailableInTheRootDirectory", UtilMisc.toMap("rootDirectory", rootDirectory), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) URISyntaxException(java.net.URISyntaxException) Security(org.apache.ofbiz.security.Security) URI(java.net.URI) URL(java.net.URL) UtilURL(org.apache.ofbiz.base.util.UtilURL) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) File(java.io.File)

Aggregations

GenericServiceException (org.apache.ofbiz.service.GenericServiceException)417 GenericValue (org.apache.ofbiz.entity.GenericValue)339 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)303 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)251 Delegator (org.apache.ofbiz.entity.Delegator)250 Locale (java.util.Locale)246 HashMap (java.util.HashMap)221 BigDecimal (java.math.BigDecimal)139 LinkedList (java.util.LinkedList)79 GeneralException (org.apache.ofbiz.base.util.GeneralException)68 Timestamp (java.sql.Timestamp)66 Map (java.util.Map)54 IOException (java.io.IOException)43 HttpSession (javax.servlet.http.HttpSession)36 ModelService (org.apache.ofbiz.service.ModelService)33 OrderReadHelper (org.apache.ofbiz.order.order.OrderReadHelper)24 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)22 ArrayList (java.util.ArrayList)21 LinkedHashMap (java.util.LinkedHashMap)20 List (java.util.List)20