Search in sources :

Example 41 with ContentItemTO

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

the class WorkflowServiceImpl method approve.

/**
 * approve workflows and schedule them as specified in the request
 *
 * @param site
 * @param request
 * @return call result
 * @throws ServiceLayerException
 */
@SuppressWarnings("unchecked")
protected ResultTO approve(String site, String request, Operation operation) {
    String approver = securityService.getCurrentUser();
    ResultTO result = new ResultTO();
    try {
        JSONObject requestObject = JSONObject.fromObject(request);
        JSONArray items = requestObject.getJSONArray(JSON_KEY_ITEMS);
        String scheduledDate = null;
        if (requestObject.containsKey(JSON_KEY_SCHEDULED_DATE)) {
            scheduledDate = requestObject.getString(JSON_KEY_SCHEDULED_DATE);
        }
        boolean isNow = (requestObject.containsKey(JSON_KEY_IS_NOW)) ? requestObject.getBoolean(JSON_KEY_IS_NOW) : false;
        String publishChannelGroupName = (requestObject.containsKey(JSON_KEY_PUBLISH_CHANNEL)) ? requestObject.getString(JSON_KEY_PUBLISH_CHANNEL) : null;
        JSONObject jsonObjectStatus = requestObject.getJSONObject(JSON_KEY_STATUS_SET);
        String statusMessage = (jsonObjectStatus != null && jsonObjectStatus.containsKey(JSON_KEY_STATUS_MESSAGE)) ? jsonObjectStatus.getString(JSON_KEY_STATUS_MESSAGE) : null;
        String submissionComment = (requestObject != null && requestObject.containsKey(JSON_KEY_SUBMISSION_COMMENT)) ? requestObject.getString(JSON_KEY_SUBMISSION_COMMENT) : "Test Go Live";
        MultiChannelPublishingContext mcpContext = new MultiChannelPublishingContext(publishChannelGroupName, statusMessage, submissionComment);
        int length = items.size();
        if (length == 0) {
            throw new ServiceLayerException("No items provided to go live.");
        }
        String responseMessageKey = null;
        SimpleDateFormat format = new SimpleDateFormat(StudioConstants.DATE_PATTERN_WORKFLOW_WITH_TZ);
        List<DmDependencyTO> submittedItems = new ArrayList<>();
        for (int index = 0; index < length; index++) {
            String stringItem = items.optString(index);
            DmDependencyTO submittedItem = null;
            submittedItem = getSubmittedItem(site, stringItem, format, scheduledDate, null);
            List<DmDependencyTO> submitForDeleteChildren = removeSubmitToDeleteChildrenForGoLive(submittedItem, operation);
            if (submittedItem.isReference()) {
                submittedItem.setReference(false);
            }
            submittedItems.add(submittedItem);
            submittedItems.addAll(submitForDeleteChildren);
        }
        switch(operation) {
            case GO_LIVE:
                if (scheduledDate != null && isNow == false) {
                    responseMessageKey = NotificationService.COMPLETE_SCHEDULE_GO_LIVE;
                } else {
                    responseMessageKey = NotificationService.COMPLETE_GO_LIVE;
                }
                List<DmDependencyTO> submitToDeleteItems = new ArrayList<>();
                List<DmDependencyTO> goLiveItems = new ArrayList<>();
                List<DmDependencyTO> renameItems = new ArrayList<>();
                for (DmDependencyTO item : submittedItems) {
                    if (item.isSubmittedForDeletion()) {
                        submitToDeleteItems.add(item);
                    } else {
                        if (!isItemRenamed(site, item)) {
                            goLiveItems.add(item);
                        } else {
                            renameItems.add(item);
                        }
                    }
                }
                if (!submitToDeleteItems.isEmpty()) {
                    doDelete(site, submitToDeleteItems, approver);
                }
                if (!goLiveItems.isEmpty()) {
                    List<DmDependencyTO> references = getRefAndChildOfDiffDateFromParent(site, goLiveItems, true);
                    List<DmDependencyTO> children = getRefAndChildOfDiffDateFromParent(site, goLiveItems, false);
                    goLiveItems.addAll(references);
                    goLiveItems.addAll(children);
                    List<String> goLivePaths = new ArrayList<>();
                    Set<String> processedPaths = new HashSet<String>();
                    for (DmDependencyTO goLiveItem : goLiveItems) {
                        resolveSubmittedPaths(site, goLiveItem, goLivePaths, processedPaths);
                    }
                    List<String> nodeRefs = new ArrayList<>();
                    goLive(site, goLiveItems, approver, mcpContext);
                }
                if (!renameItems.isEmpty()) {
                    List<String> renamePaths = new ArrayList<>();
                    List<DmDependencyTO> renamedChildren = new ArrayList<>();
                    for (DmDependencyTO renameItem : renameItems) {
                        renamedChildren.addAll(getChildrenForRenamedItem(site, renameItem));
                        renamePaths.add(renameItem.getUri());
                        objectStateService.setSystemProcessing(site, renameItem.getUri(), true);
                    }
                    for (DmDependencyTO renamedChild : renamedChildren) {
                        renamePaths.add(renamedChild.getUri());
                        objectStateService.setSystemProcessing(site, renamedChild.getUri(), true);
                    }
                    renameItems.addAll(renamedChildren);
                    // Set proper information of all renameItems before send them to GoLive
                    for (int i = 0; i < renameItems.size(); i++) {
                        DmDependencyTO renamedItem = renameItems.get(i);
                        if (renamedItem.getScheduledDate() != null && renamedItem.getScheduledDate().isAfter(ZonedDateTime.now(ZoneOffset.UTC))) {
                            renamedItem.setNow(false);
                        } else {
                            renamedItem.setNow(true);
                        }
                        renameItems.set(i, renamedItem);
                    }
                    goLive(site, renameItems, approver, mcpContext);
                }
                break;
            case DELETE:
                responseMessageKey = NotificationService.COMPLETE_DELETE;
                List<String> deletePaths = new ArrayList<>();
                List<String> nodeRefs = new ArrayList<String>();
                for (DmDependencyTO deletedItem : submittedItems) {
                    deletePaths.add(deletedItem.getUri());
                    ContentItemTO contentItem = contentService.getContentItem(site, deletedItem.getUri());
                    if (contentItem != null) {
                    // nodeRefs.add(nodeRef.getId());
                    }
                }
                doDelete(site, submittedItems, approver);
        }
        result.setSuccess(true);
        result.setStatus(200);
        result.setMessage(notificationService.getNotificationMessage(site, NotificationMessageType.CompleteMessages, responseMessageKey, Locale.ENGLISH));
    } catch (JSONException e) {
        logger.error("error performing operation " + operation + " " + e);
        result.setSuccess(false);
        result.setMessage(e.getMessage());
    } catch (ServiceLayerException e) {
        logger.error("error performing operation " + operation + " " + e);
        result.setSuccess(false);
        result.setMessage(e.getMessage());
    }
    return result;
}
Also used : JSONArray(net.sf.json.JSONArray) ArrayList(java.util.ArrayList) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) JSONException(net.sf.json.JSONException) DmDependencyTO(org.craftercms.studio.api.v1.to.DmDependencyTO) ResultTO(org.craftercms.studio.api.v1.to.ResultTO) ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) MultiChannelPublishingContext(org.craftercms.studio.api.v1.service.workflow.context.MultiChannelPublishingContext) JSONObject(net.sf.json.JSONObject) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet)

Example 42 with ContentItemTO

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

the class WorkflowServiceImpl method getGoLiveItems.

@Override
@ValidateParams
public Map<String, Object> getGoLiveItems(@ValidateStringParam(name = "site") String site, @ValidateStringParam(name = "sort") String sort, boolean ascending) throws ServiceLayerException {
    DmContentItemComparator comparator = new DmContentItemComparator(sort, ascending, false, false);
    List<ContentItemTO> items = getGoLiveItems(site, comparator);
    int total = 0;
    if (items != null) {
        for (ContentItemTO item : items) {
            total += item.getNumOfChildren();
        }
    }
    Map<String, Object> result = new HashMap<>();
    result.put(StudioConstants.PROPERTY_TOTAL, total);
    result.put(StudioConstants.PROPERTY_SORTED_BY, sort);
    result.put(StudioConstants.PROPERTY_SORT_ASCENDING, String.valueOf(ascending));
    result.put(StudioConstants.PROPERTY_DOCUMENTS, items);
    return result;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) DmContentItemComparator(org.craftercms.studio.api.v1.util.DmContentItemComparator) HashMap(java.util.HashMap) JSONObject(net.sf.json.JSONObject) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 43 with ContentItemTO

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

the class WorkflowServiceImpl method isRescheduleRequest.

@Override
@ValidateParams
public boolean isRescheduleRequest(DmDependencyTO dependencyTO, @ValidateStringParam(name = "site") String site) {
    if ((dependencyTO.isDeleted() || (!dependencyTO.isSubmitted() && !dependencyTO.isInProgress()))) {
        ContentItemTO to = contentService.getContentItem(site, dependencyTO.getUri());
        ZonedDateTime newDate = dependencyTO.getScheduledDate();
        ZonedDateTime oldDate = to.getScheduledDate();
        return !areEqual(oldDate, newDate);
    }
    return false;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ZonedDateTime(java.time.ZonedDateTime) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 44 with ContentItemTO

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

the class WorkflowServiceImpl method getGoLiveItems.

protected List<ContentItemTO> getGoLiveItems(final String site, final DmContentItemComparator comparator) throws ServiceLayerException {
    List<String> displayPatterns = servicesConfig.getDisplayInWidgetPathPatterns(site);
    List<ContentItemTO> categoryItems = getCategoryItems(site);
    GoLiveQueue queue = new GoLiveQueue();
    fillQueue(site, queue, null);
    Set<ContentItemTO> queueItems = queue.getQueue();
    ContentItemTO.ChildFilter childFilter = new GoLiveQueueChildFilter(queue);
    GoLiveQueueOrganizer goLiveQueueOrganizer = new GoLiveQueueOrganizer(contentService, childFilter);
    for (ContentItemTO queueItem : queueItems) {
        if (queueItem.getLastEditDate() != null) {
            queueItem.setEventDate(queueItem.getLastEditDate());
        }
        goLiveQueueOrganizer.addToGoLiveItems(site, queueItem, categoryItems, comparator, false, displayPatterns);
    }
    return categoryItems;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) GoLiveQueueChildFilter(org.craftercms.studio.api.v1.to.GoLiveQueueChildFilter) GoLiveQueueOrganizer(org.craftercms.studio.impl.v1.util.GoLiveQueueOrganizer) GoLiveQueue(org.craftercms.studio.api.v1.to.GoLiveQueue)

Example 45 with ContentItemTO

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

the class WorkflowServiceImpl method getInProgressItems.

@Override
@ValidateParams
public Map<String, Object> getInProgressItems(@ValidateStringParam(name = "site") String site, @ValidateStringParam(name = "sort") String sort, boolean ascending, boolean inProgressOnly) throws ServiceLayerException {
    DmContentItemComparator comparator = new DmContentItemComparator(sort, ascending, true, true);
    comparator.setSecondLevelCompareRequired(true);
    comparator.setSecondLevelSortBy(DmContentItemComparator.SORT_PATH);
    List<ContentItemTO> items = getInProgressItems(site, comparator, inProgressOnly);
    JSONObject jsonObject = new JSONObject();
    int total = 0;
    if (items != null) {
        for (ContentItemTO item : items) {
            total += item.getNumOfChildren();
        }
    }
    Map<String, Object> result = new HashMap<>();
    result.put(StudioConstants.PROPERTY_TOTAL, total);
    result.put(StudioConstants.PROPERTY_SORTED_BY, sort);
    result.put(StudioConstants.PROPERTY_SORT_ASCENDING, String.valueOf(ascending));
    result.put(StudioConstants.PROPERTY_DOCUMENTS, items);
    return result;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) JSONObject(net.sf.json.JSONObject) DmContentItemComparator(org.craftercms.studio.api.v1.util.DmContentItemComparator) HashMap(java.util.HashMap) JSONObject(net.sf.json.JSONObject) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Aggregations

ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)60 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)19 HashMap (java.util.HashMap)16 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)15 ArrayList (java.util.ArrayList)10 ItemState (org.craftercms.studio.api.v1.dal.ItemState)10 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)10 JSONObject (net.sf.json.JSONObject)6 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)6 HashSet (java.util.HashSet)5 JSONException (net.sf.json.JSONException)5 ItemMetadata (org.craftercms.studio.api.v1.dal.ItemMetadata)5 ZonedDateTime (java.time.ZonedDateTime)4 CryptoException (org.craftercms.commons.crypto.CryptoException)4 EntitlementException (org.craftercms.commons.entitlements.exception.EntitlementException)4 DeploymentException (org.craftercms.studio.api.v1.service.deployment.DeploymentException)4 DocumentException (org.dom4j.DocumentException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)3