Search in sources :

Example 76 with GeneralException

use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.

the class ContentServices method renderSubContentAsText.

/**
 * Get and render subcontent associated with template id and mapkey. If subContentId is supplied, that content will be rendered without searching for other
 * matching content.
 */
public static Map<String, Object> renderSubContentAsText(DispatchContext dctx, Map<String, ? extends Object> context) {
    Map<String, Object> results = new HashMap<String, Object>();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Map<String, Object> templateContext = UtilGenerics.checkMap(context.get("templateContext"));
    String contentId = (String) context.get("contentId");
    if (templateContext != null && UtilValidate.isEmpty(contentId)) {
        contentId = (String) templateContext.get("contentId");
    }
    String mapKey = (String) context.get("mapKey");
    if (templateContext != null && UtilValidate.isEmpty(mapKey)) {
        mapKey = (String) templateContext.get("mapKey");
    }
    String mimeTypeId = (String) context.get("mimeTypeId");
    if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) {
        mimeTypeId = (String) templateContext.get("mimeTypeId");
    }
    Locale locale = (Locale) context.get("locale");
    if (templateContext != null && locale == null) {
        locale = (Locale) templateContext.get("locale");
    }
    Writer out = (Writer) context.get("outWriter");
    Writer outWriter = new StringWriter();
    if (templateContext == null) {
        templateContext = new HashMap<String, Object>();
    }
    try {
        ContentWorker.renderSubContentAsText(dispatcher, contentId, outWriter, mapKey, templateContext, locale, mimeTypeId, true);
        out.write(outWriter.toString());
        results.put("textData", outWriter.toString());
    } catch (GeneralException e) {
        Debug.logError(e, "Error rendering sub-content text", module);
        return ServiceUtil.returnError(e.toString());
    } catch (IOException e) {
        Debug.logError(e, "Error rendering sub-content text", module);
        return ServiceUtil.returnError(e.toString());
    }
    return results;
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 77 with GeneralException

use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.

the class ContentServices method renderContentAsText.

/**
 * Get and render subcontent associated with template id and mapkey. If subContentId is supplied, that content will be rendered without searching for other
 * matching content.
 */
public static Map<String, Object> renderContentAsText(DispatchContext dctx, Map<String, ? extends Object> context) {
    Map<String, Object> results = new HashMap<String, Object>();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Writer out = (Writer) context.get("outWriter");
    Map<String, Object> templateContext = UtilGenerics.checkMap(context.get("templateContext"));
    String contentId = (String) context.get("contentId");
    if (templateContext != null && UtilValidate.isEmpty(contentId)) {
        contentId = (String) templateContext.get("contentId");
    }
    String mimeTypeId = (String) context.get("mimeTypeId");
    if (templateContext != null && UtilValidate.isEmpty(mimeTypeId)) {
        mimeTypeId = (String) templateContext.get("mimeTypeId");
    }
    Locale locale = (Locale) context.get("locale");
    if (templateContext != null && locale == null) {
        locale = (Locale) templateContext.get("locale");
    }
    if (templateContext == null) {
        templateContext = new HashMap<String, Object>();
    }
    Writer outWriter = new StringWriter();
    GenericValue view = (GenericValue) context.get("subContentDataResourceView");
    if (view != null && view.containsKey("contentId")) {
        contentId = view.getString("contentId");
    }
    try {
        ContentWorker.renderContentAsText(dispatcher, contentId, outWriter, templateContext, locale, mimeTypeId, null, null, true);
        if (out != null)
            out.write(outWriter.toString());
        results.put("textData", outWriter.toString());
    } catch (GeneralException e) {
        Debug.logError(e, "Error rendering sub-content text", module);
        return ServiceUtil.returnError(e.toString());
    } catch (IOException e) {
        Debug.logError(e, "Error rendering sub-content text", module);
        return ServiceUtil.returnError(e.toString());
    }
    return results;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 78 with GeneralException

use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.

the class ContentWorker method getCurrentContent.

public static GenericValue getCurrentContent(Delegator delegator, List<Map<String, ? extends Object>> trail, GenericValue userLogin, Map<String, Object> ctx, Boolean nullThruDatesOnly, String contentAssocPredicateId) throws GeneralException {
    String contentId = (String) ctx.get("contentId");
    String subContentId = (String) ctx.get("subContentId");
    String mapKey = (String) ctx.get("mapKey");
    Timestamp fromDate = UtilDateTime.nowTimestamp();
    List<String> assocTypes = null;
    List<Map<String, Object>> passedGlobalNodeTrail = null;
    GenericValue currentContent = null;
    String viewContentId = null;
    if (UtilValidate.isNotEmpty(trail)) {
        passedGlobalNodeTrail = UtilGenerics.checkList(UtilMisc.makeListWritable(trail));
    } else {
        passedGlobalNodeTrail = new LinkedList<Map<String, Object>>();
    }
    int sz = passedGlobalNodeTrail.size();
    if (sz > 0) {
        Map<String, Object> nd = passedGlobalNodeTrail.get(sz - 1);
        if (nd != null)
            currentContent = (GenericValue) nd.get("value");
        if (currentContent != null)
            viewContentId = (String) currentContent.get("contentId");
    }
    if (UtilValidate.isNotEmpty(subContentId)) {
        ctx.put("subContentId", subContentId);
        ctx.put("contentId", null);
        if (viewContentId != null && viewContentId.equals(subContentId)) {
            return currentContent;
        }
    } else {
        ctx.put("contentId", contentId);
        ctx.put("subContentId", null);
        if (viewContentId != null && viewContentId.equals(contentId)) {
            return currentContent;
        }
    }
    if (UtilValidate.isNotEmpty(contentId) || UtilValidate.isNotEmpty(subContentId)) {
        try {
            currentContent = ContentWorker.getSubContentCache(delegator, contentId, mapKey, subContentId, userLogin, assocTypes, fromDate, nullThruDatesOnly, contentAssocPredicateId);
            Map<String, Object> node = ContentWorker.makeNode(currentContent);
            passedGlobalNodeTrail.add(node);
        } catch (GenericEntityException e) {
            throw new GeneralException(e.getMessage());
        }
    }
    ctx.put("globalNodeTrail", passedGlobalNodeTrail);
    ctx.put("indent", Integer.valueOf(sz));
    return currentContent;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Timestamp(java.sql.Timestamp) HashMap(java.util.HashMap) Map(java.util.Map)

Example 79 with GeneralException

use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.

the class PdfSurveyServices method getAcroFieldsFromPdf.

/**
 */
public static Map<String, Object> getAcroFieldsFromPdf(DispatchContext dctx, Map<String, ? extends Object> context) {
    Map<String, Object> acroFieldMap = new HashMap<>();
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Delegator delegator = dctx.getDelegator();
        ByteBuffer byteBuffer = getInputByteBuffer(context, delegator);
        PdfReader r = new PdfReader(byteBuffer.array());
        PdfStamper s = new PdfStamper(r, os);
        AcroFields fs = s.getAcroFields();
        Map<String, Object> map = UtilGenerics.checkMap(fs.getFields());
        s.setFormFlattening(true);
        for (String fieldName : map.keySet()) {
            String parmValue = fs.getField(fieldName);
            acroFieldMap.put(fieldName, parmValue);
        }
    } catch (DocumentException | GeneralException | IOException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    Map<String, Object> results = ServiceUtil.returnSuccess();
    results.put("acroFieldMap", acroFieldMap);
    return results;
}
Also used : GeneralException(org.apache.ofbiz.base.util.GeneralException) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PdfReader(com.lowagie.text.pdf.PdfReader) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Delegator(org.apache.ofbiz.entity.Delegator) PdfStamper(com.lowagie.text.pdf.PdfStamper) DocumentException(com.lowagie.text.DocumentException) AcroFields(com.lowagie.text.pdf.AcroFields) PdfObject(com.lowagie.text.pdf.PdfObject)

Example 80 with GeneralException

use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.

the class PdfSurveyServices method buildSurveyFromPdf.

/**
 */
public static Map<String, Object> buildSurveyFromPdf(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    String surveyId = null;
    try {
        String surveyName = (String) context.get("surveyName");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ByteBuffer byteBuffer = getInputByteBuffer(context, delegator);
        PdfReader pdfReader = new PdfReader(byteBuffer.array());
        PdfStamper pdfStamper = new PdfStamper(pdfReader, os);
        AcroFields acroFields = pdfStamper.getAcroFields();
        Map<String, Object> acroFieldMap = UtilGenerics.checkMap(acroFields.getFields());
        String contentId = (String) context.get("contentId");
        GenericValue survey = null;
        surveyId = (String) context.get("surveyId");
        if (UtilValidate.isEmpty(surveyId)) {
            survey = delegator.makeValue("Survey", UtilMisc.toMap("surveyName", surveyName));
            survey.set("surveyId", surveyId);
            survey.set("allowMultiple", "Y");
            survey.set("allowUpdate", "Y");
            survey = delegator.createSetNextSeqId(survey);
            surveyId = survey.getString("surveyId");
        }
        // create a SurveyQuestionCategory to put the questions in
        Map<String, Object> createCategoryResultMap = dispatcher.runSync("createSurveyQuestionCategory", UtilMisc.<String, Object>toMap("description", "From AcroForm in Content [" + contentId + "] for Survey [" + surveyId + "]", "userLogin", userLogin));
        if (ServiceUtil.isError(createCategoryResultMap)) {
            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createCategoryResultMap));
        }
        String surveyQuestionCategoryId = (String) createCategoryResultMap.get("surveyQuestionCategoryId");
        pdfStamper.setFormFlattening(true);
        for (String fieldName : acroFieldMap.keySet()) {
            AcroFields.Item item = acroFields.getFieldItem(fieldName);
            int type = acroFields.getFieldType(fieldName);
            String value = acroFields.getField(fieldName);
            Debug.logInfo("fieldName:" + fieldName + "; item: " + item + "; value: " + value, module);
            GenericValue surveyQuestion = delegator.makeValue("SurveyQuestion", UtilMisc.toMap("question", fieldName));
            String surveyQuestionId = delegator.getNextSeqId("SurveyQuestion");
            surveyQuestion.set("surveyQuestionId", surveyQuestionId);
            surveyQuestion.set("surveyQuestionCategoryId", surveyQuestionCategoryId);
            if (type == AcroFields.FIELD_TYPE_TEXT) {
                surveyQuestion.set("surveyQuestionTypeId", "TEXT_SHORT");
            } else if (type == AcroFields.FIELD_TYPE_RADIOBUTTON) {
                surveyQuestion.set("surveyQuestionTypeId", "OPTION");
            } else if (type == AcroFields.FIELD_TYPE_LIST || type == AcroFields.FIELD_TYPE_COMBO) {
                surveyQuestion.set("surveyQuestionTypeId", "OPTION");
            // TODO: handle these specially with the acroFields.getListOptionDisplay (and getListOptionExport?)
            } else {
                surveyQuestion.set("surveyQuestionTypeId", "TEXT_SHORT");
                Debug.logWarning("Building Survey from PDF, fieldName=[" + fieldName + "]: don't know how to handle field type: " + type + "; defaulting to short text", module);
            }
            // ==== create a good sequenceNum based on tab order or if no tab order then the page location
            Integer tabPage = item.getPage(0);
            Integer tabOrder = item.getTabOrder(0);
            Debug.logInfo("tabPage=" + tabPage + ", tabOrder=" + tabOrder, module);
            // array of float  multiple of 5. For each of this groups the values are: [page, llx, lly, urx, ury]
            float[] fieldPositions = acroFields.getFieldPositions(fieldName);
            float fieldPage = fieldPositions[0];
            float fieldLlx = fieldPositions[1];
            float fieldLly = fieldPositions[2];
            float fieldUrx = fieldPositions[3];
            float fieldUry = fieldPositions[4];
            Debug.logInfo("fieldPage=" + fieldPage + ", fieldLlx=" + fieldLlx + ", fieldLly=" + fieldLly + ", fieldUrx=" + fieldUrx + ", fieldUry=" + fieldUry, module);
            Long sequenceNum = null;
            if (tabPage != null && tabOrder != null) {
                sequenceNum = Long.valueOf(tabPage.intValue() * 1000 + tabOrder.intValue());
                Debug.logInfo("tabPage=" + tabPage + ", tabOrder=" + tabOrder + ", sequenceNum=" + sequenceNum, module);
            } else if (fieldPositions.length > 0) {
                sequenceNum = Long.valueOf((long) fieldPage * 10000 + (long) fieldLly * 1000 + (long) fieldLlx);
                Debug.logInfo("fieldPage=" + fieldPage + ", fieldLlx=" + fieldLlx + ", fieldLly=" + fieldLly + ", fieldUrx=" + fieldUrx + ", fieldUry=" + fieldUry + ", sequenceNum=" + sequenceNum, module);
            }
            // TODO: need to find something better to put into these fields...
            String annotation = null;
            for (int k = 0; k < item.size(); ++k) {
                PdfDictionary dict = item.getWidget(k);
                // if the "/Type" value is "/Annot", then get the value of "/TU" for the annotation
                PdfObject typeValue = null;
                PdfObject tuValue = null;
                Set<PdfName> dictKeys = UtilGenerics.checkSet(dict.getKeys());
                for (PdfName dictKeyName : dictKeys) {
                    PdfObject dictObject = dict.get(dictKeyName);
                    if ("/Type".equals(dictKeyName.toString())) {
                        typeValue = dictObject;
                    } else if ("/TU".equals(dictKeyName.toString())) {
                        tuValue = dictObject;
                    }
                }
                if (tuValue != null && typeValue != null && "/Annot".equals(typeValue.toString())) {
                    annotation = tuValue.toString();
                }
            }
            surveyQuestion.set("description", fieldName);
            if (UtilValidate.isNotEmpty(annotation)) {
                surveyQuestion.set("question", annotation);
            } else {
                surveyQuestion.set("question", fieldName);
            }
            GenericValue surveyQuestionAppl = delegator.makeValue("SurveyQuestionAppl", UtilMisc.toMap("surveyId", surveyId, "surveyQuestionId", surveyQuestionId));
            surveyQuestionAppl.set("fromDate", nowTimestamp);
            surveyQuestionAppl.set("externalFieldRef", fieldName);
            if (sequenceNum != null) {
                surveyQuestionAppl.set("sequenceNum", sequenceNum);
            }
            surveyQuestion.create();
            surveyQuestionAppl.create();
        }
        pdfStamper.close();
        if (UtilValidate.isNotEmpty(contentId)) {
            survey = EntityQuery.use(delegator).from("Survey").where("surveyId", surveyId).queryOne();
            survey.set("acroFormContentId", contentId);
            survey.store();
        }
    } catch (GeneralException | DocumentException | IOException e) {
        Debug.logError(e, "Error generating PDF: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPDFGeneratingError", UtilMisc.toMap("errorString", e.toString()), locale));
    }
    Map<String, Object> results = ServiceUtil.returnSuccess();
    results.put("surveyId", surveyId);
    return results;
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) PdfObject(com.lowagie.text.pdf.PdfObject) PdfReader(com.lowagie.text.pdf.PdfReader) Timestamp(java.sql.Timestamp) DocumentException(com.lowagie.text.DocumentException) GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) PdfName(com.lowagie.text.pdf.PdfName) PdfDictionary(com.lowagie.text.pdf.PdfDictionary) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Delegator(org.apache.ofbiz.entity.Delegator) PdfStamper(com.lowagie.text.pdf.PdfStamper) AcroFields(com.lowagie.text.pdf.AcroFields) PdfObject(com.lowagie.text.pdf.PdfObject)

Aggregations

GeneralException (org.apache.ofbiz.base.util.GeneralException)216 GenericValue (org.apache.ofbiz.entity.GenericValue)133 Delegator (org.apache.ofbiz.entity.Delegator)101 Locale (java.util.Locale)81 HashMap (java.util.HashMap)71 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)68 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)68 IOException (java.io.IOException)65 BigDecimal (java.math.BigDecimal)55 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)54 Writer (java.io.Writer)29 LinkedList (java.util.LinkedList)29 Map (java.util.Map)29 Timestamp (java.sql.Timestamp)26 StringWriter (java.io.StringWriter)19 Environment (freemarker.core.Environment)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ShoppingCart (org.apache.ofbiz.order.shoppingcart.ShoppingCart)14 HttpSession (javax.servlet.http.HttpSession)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)13