public static String recordReportContent()

in birt/src/main/java/org/apache/ofbiz/birt/BirtWorker.java [205:324]


    public static String recordReportContent(Delegator delegator, LocalDispatcher dispatcher, Map<String, Object> context) throws GeneralException {
        Locale locale = (Locale) context.get("locale");
        String description = (String) context.get("description");
        String reportName = (String) context.get("reportName");
        GenericValue userLogin = (GenericValue) context.get("userLogin");
        String entityViewName = (String) context.get("entityViewName");
        String serviceName = (String) context.get("serviceName");
        String masterContentId = (String) context.get("masterContentId");
        String dataResourceId = delegator.getNextSeqId("DataResource");
        String contentId = delegator.getNextSeqId("Content");
        context.put("contentId", contentId);

        if (UtilValidate.isEmpty(serviceName) && UtilValidate.isEmpty(entityViewName)) {
            throw new GenericServiceException("Service and entity name cannot be both empty");
        }

        String modelElementName = null;
        String workflowType = null;
        if (UtilValidate.isEmpty(serviceName)) {
            modelElementName = entityViewName;
            workflowType = "Entity";
        } else {
            modelElementName = serviceName;
            workflowType = "Service";
        }

        EntityCondition entityConditionRpt = EntityCondition.makeCondition("contentTypeId", "RPTDESIGN");
        String templatePathLocation = BirtUtil.resolveTemplatePathLocation();
        File templatePathLocationDir = new File(templatePathLocation);
        if (!templatePathLocationDir.exists()) {
            boolean created = templatePathLocationDir.mkdirs();
            if (!created) {
                throw new GeneralException(UtilProperties.getMessage(RES_ERROR, "BirtErrorCannotLocateReportFolder", locale));
            }
        }
        int i = 0;
        String templateFileLocation = null;
        EntityCondition ecl = null;
        do {
            StringBuffer rptDesignNameSb = new StringBuffer(templatePathLocation);
            rptDesignNameSb.append(BirtUtil.encodeReportName(reportName));
            rptDesignNameSb.append("_").append(i);
            rptDesignNameSb.append(".rptdesign");
            templateFileLocation = rptDesignNameSb.toString();
            EntityCondition entityConditionOnName = EntityCondition.makeCondition("drObjectInfo", templateFileLocation);
            ecl = EntityCondition.makeCondition(UtilMisc.toList(entityConditionRpt, entityConditionOnName));
            i++;
        } while (EntityQuery.use(delegator).from("ContentDataResourceView").where(ecl).queryCount() > 0);

        //resolve the initial form structure from master content
        Map<String, Object> resultElectronicText = dispatcher.runSync("getElectronicText", UtilMisc.toMap("contentId", masterContentId, "locale",
                locale, "userLogin", userLogin));
        if (ServiceUtil.isError(resultElectronicText)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(resultElectronicText));
        }
        String reportForm = (String) resultElectronicText.get("textData");
        if (!reportForm.startsWith("<?xml")) {
            StringBuffer xmlHeaderForm = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlHeaderForm.append("<forms xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                    + "xmlns=\"http://ofbiz.apache.org/Widget-Form\" "
                    + "xsi:schemaLocation=\"http://ofbiz.apache.org/Widget-Form "
                    + "http://ofbiz.apache.org/dtds/widget-form.xsd\">");
            xmlHeaderForm.append(reportForm);
            xmlHeaderForm.append("</forms>");
            reportForm = xmlHeaderForm.toString();
        }
        FlexibleStringExpander reportFormExpd = FlexibleStringExpander.getInstance(reportForm);
        reportForm = reportFormExpd.expandString(context);

        //create content and dataressource strucutre
        Map<String, Object> result = null;
        result = dispatcher.runSync("createDataResource", UtilMisc.toMap("dataResourceId", dataResourceId, "dataResourceTypeId", "ELECTRONIC_TEXT",
                "dataTemplateTypeId", "FORM_COMBINED", "userLogin", userLogin));
        if (ServiceUtil.isError(result)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(result));
        }
        result = dispatcher.runSync("createElectronicTextForm", UtilMisc.toMap("dataResourceId", dataResourceId, "textData", reportForm, "userLogin",
                userLogin));
        if (ServiceUtil.isError(result)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(result));
        }
        result = dispatcher.runSync("createContent", UtilMisc.toMap("contentId", contentId, "contentTypeId", "FLEXIBLE_REPORT", "dataResourceId",
                dataResourceId, "statusId", "CTNT_IN_PROGRESS", "contentName", reportName, "description", description, "userLogin", userLogin));
        if (ServiceUtil.isError(result)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(result));
        }
        String dataResourceIdRpt = delegator.getNextSeqId("DataResource");
        String contentIdRpt = delegator.getNextSeqId("Content");
        String rptDesignName = BirtUtil.encodeReportName(reportName);
        if (!rptDesignName.endsWith(".rptdesign")) {
            rptDesignName = rptDesignName.concat(".rptdesign");
        }
        result = dispatcher.runSync("createDataResource", UtilMisc.toMap("dataResourceId", dataResourceIdRpt, "dataResourceTypeId", "LOCAL_FILE",
                "mimeTypeId", "text/rptdesign", "dataResourceName", rptDesignName, "objectInfo", templateFileLocation, "userLogin", userLogin));
        if (ServiceUtil.isError(result)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(result));
        }
        result = dispatcher.runSync("createContent", UtilMisc.toMap("contentId", contentIdRpt, "contentTypeId", "RPTDESIGN", "dataResourceId",
                dataResourceIdRpt, "statusId", "CTNT_PUBLISHED", "contentName", reportName, "description", description + " (.rptDesign file)",
                "userLogin", userLogin));
        if (ServiceUtil.isError(result)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(result));
        }
        result = dispatcher.runSync("createContentAssoc", UtilMisc.toMap("contentId", masterContentId, "contentIdTo", contentId,
                "contentAssocTypeId", "SUB_CONTENT", "userLogin", userLogin));
        if (ServiceUtil.isError(result)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(result));
        }
        result = dispatcher.runSync("createContentAssoc", UtilMisc.toMap("contentId", contentId, "contentIdTo", contentIdRpt, "contentAssocTypeId",
                "SUB_CONTENT", "userLogin", userLogin));
        if (ServiceUtil.isError(result)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(result));
        }
        result = dispatcher.runSync("createContentAttribute", UtilMisc.toMap("contentId", contentId, "attrName", workflowType, "attrValue",
                modelElementName, "userLogin", userLogin));
        if (ServiceUtil.isError(result)) {
            throw new GeneralException(ServiceUtil.getErrorMessage(result));
        }
        return contentId;
    }