Search in sources :

Example 1 with DmError

use of org.craftercms.studio.api.v1.to.DmError in project studio by craftercms.

the class WorkflowServiceImpl method submitForApproval.

@SuppressWarnings("unchecked")
protected ResultTO submitForApproval(final String site, String submittedBy, final String request, final boolean delete) throws ServiceLayerException {
    RequestContext requestContext = RequestContextBuilder.buildSubmitContext(site, submittedBy);
    ResultTO result = new ResultTO();
    try {
        SimpleDateFormat format = new SimpleDateFormat(StudioConstants.DATE_PATTERN_WORKFLOW_WITH_TZ);
        JSONObject requestObject = JSONObject.fromObject(request);
        JSONArray items = requestObject.getJSONArray(JSON_KEY_ITEMS);
        int length = items.size();
        if (length > 0) {
            for (int index = 0; index < length; index++) {
                objectStateService.setSystemProcessing(site, items.optString(index), true);
            }
        }
        boolean isNow = (requestObject.containsKey(JSON_KEY_SCHEDULE)) ? StringUtils.equalsIgnoreCase(requestObject.getString(JSON_KEY_SCHEDULE), JSON_KEY_IS_NOW) : false;
        ZonedDateTime scheduledDate = null;
        if (!isNow) {
            scheduledDate = (requestObject.containsKey(JSON_KEY_SCHEDULED_DATE)) ? getScheduledDate(site, format, requestObject.getString(JSON_KEY_SCHEDULED_DATE)) : null;
        }
        boolean sendEmail = (requestObject.containsKey(JSON_KEY_SEND_EMAIL)) ? requestObject.getBoolean(JSON_KEY_SEND_EMAIL) : false;
        String environment = (requestObject != null && requestObject.containsKey(JSON_KEY_ENVIRONMENT)) ? requestObject.getString(JSON_KEY_ENVIRONMENT) : null;
        String submissionComment = (requestObject != null && requestObject.containsKey(JSON_KEY_SUBMISSION_COMMENT)) ? requestObject.getString(JSON_KEY_SUBMISSION_COMMENT) : null;
        // TODO: check scheduled date to make sure it is not null when isNow
        // = true and also it is not past
        String schDate = null;
        if (requestObject.containsKey(JSON_KEY_SCHEDULED_DATE)) {
            schDate = requestObject.getString(JSON_KEY_SCHEDULED_DATE);
        }
        if (length > 0) {
            List<DmDependencyTO> submittedItems = new ArrayList<DmDependencyTO>();
            for (int index = 0; index < length; index++) {
                String stringItem = items.optString(index);
                DmDependencyTO submittedItem = getSubmittedItem(site, stringItem, format, schDate, null);
                String user = submittedBy;
                submittedItems.add(submittedItem);
                if (delete) {
                    submittedItem.setSubmittedForDeletion(true);
                }
            }
            submittedItems.addAll(addDependenciesForSubmitForApproval(site, submittedItems, format, schDate));
            List<String> submittedPaths = new ArrayList<String>();
            for (DmDependencyTO goLiveItem : submittedItems) {
                submittedPaths.add(goLiveItem.getUri());
                objectStateService.setSystemProcessing(site, goLiveItem.getUri(), true);
                DependencyRules rule = new DependencyRules(site);
                rule.setObjectStateService(objectStateService);
                rule.setContentService(contentService);
                Set<DmDependencyTO> depSet = rule.applySubmitRule(goLiveItem);
                for (DmDependencyTO dep : depSet) {
                    submittedPaths.add(dep.getUri());
                    objectStateService.setSystemProcessing(site, dep.getUri(), true);
                }
            }
            List<DmError> errors = submitToGoLive(submittedItems, scheduledDate, sendEmail, delete, requestContext, submissionComment, environment);
            SiteFeed siteFeed = siteService.getSite(site);
            AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
            auditLog.setOperation(OPERATION_REQUEST_PUBLISH);
            auditLog.setActorId(submittedBy);
            auditLog.setSiteId(siteFeed.getId());
            auditLog.setPrimaryTargetId(site);
            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
            auditLog.setPrimaryTargetValue(site);
            auditServiceInternal.insertAuditLog(auditLog);
            result.setSuccess(true);
            result.setStatus(200);
            result.setMessage(notificationService.getNotificationMessage(site, NotificationMessageType.CompleteMessages, COMPLETE_SUBMIT_TO_GO_LIVE_MSG, Locale.ENGLISH));
            for (String relativePath : submittedPaths) {
                objectStateService.setSystemProcessing(site, relativePath, false);
            }
        }
    } catch (Exception e) {
        result.setSuccess(false);
        result.setMessage(e.getMessage());
        logger.error("Error while submitting content for approval.", e);
    }
    return result;
}
Also used : JSONArray(net.sf.json.JSONArray) ArrayList(java.util.ArrayList) DependencyRules(org.craftercms.studio.api.v1.service.dependency.DependencyRules) DmDependencyTO(org.craftercms.studio.api.v1.to.DmDependencyTO) ResultTO(org.craftercms.studio.api.v1.to.ResultTO) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) JSONException(net.sf.json.JSONException) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) JSONObject(net.sf.json.JSONObject) ZonedDateTime(java.time.ZonedDateTime) DmError(org.craftercms.studio.api.v1.to.DmError) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) RequestContext(org.craftercms.studio.api.v1.service.workflow.context.RequestContext) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with DmError

use of org.craftercms.studio.api.v1.to.DmError in project studio by craftercms.

the class WorkflowServiceImpl method submitToGoLive.

protected List<DmError> submitToGoLive(List<DmDependencyTO> submittedItems, ZonedDateTime scheduledDate, boolean sendEmail, boolean submitForDeletion, RequestContext requestContext, String submissionComment, String environment) throws ServiceLayerException {
    List<DmError> errors = new ArrayList<DmError>();
    String site = requestContext.getSite();
    String submittedBy = requestContext.getUser();
    for (DmDependencyTO submittedItem : submittedItems) {
        try {
            DependencyRules rule = new DependencyRules(site);
            rule.setContentService(contentService);
            rule.setObjectStateService(objectStateService);
            submitThisAndReferredComponents(submittedItem, site, scheduledDate, sendEmail, submitForDeletion, submittedBy, rule, submissionComment, environment);
            List<DmDependencyTO> children = submittedItem.getChildren();
            if (children != null && !submitForDeletion) {
                for (DmDependencyTO child : children) {
                    if (!child.isReference()) {
                        submitThisAndReferredComponents(child, site, scheduledDate, sendEmail, submitForDeletion, submittedBy, rule, submissionComment, environment);
                    }
                }
            }
        } catch (ContentNotFoundException e) {
            errors.add(new DmError(site, submittedItem.getUri(), e));
        }
    }
    notificationService.notifyApprovesContentSubmission(site, null, getDeploymentPaths(submittedItems), submittedBy, scheduledDate, submitForDeletion, submissionComment, Locale.ENGLISH);
    return errors;
}
Also used : ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) DmError(org.craftercms.studio.api.v1.to.DmError) ArrayList(java.util.ArrayList) DependencyRules(org.craftercms.studio.api.v1.service.dependency.DependencyRules) DmDependencyTO(org.craftercms.studio.api.v1.to.DmDependencyTO)

Aggregations

ArrayList (java.util.ArrayList)2 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)2 DependencyRules (org.craftercms.studio.api.v1.service.dependency.DependencyRules)2 DmDependencyTO (org.craftercms.studio.api.v1.to.DmDependencyTO)2 DmError (org.craftercms.studio.api.v1.to.DmError)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ZonedDateTime (java.time.ZonedDateTime)1 JSONArray (net.sf.json.JSONArray)1 JSONException (net.sf.json.JSONException)1 JSONObject (net.sf.json.JSONObject)1 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)1 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)1 DeploymentException (org.craftercms.studio.api.v1.service.deployment.DeploymentException)1 RequestContext (org.craftercms.studio.api.v1.service.workflow.context.RequestContext)1 ResultTO (org.craftercms.studio.api.v1.to.ResultTO)1 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)1