Search in sources :

Example 11 with ResultTO

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

the class FormDmContentProcessor method writeContent.

protected void writeContent(PipelineContent content, ResultTO result) throws ServiceLayerException {
    String user = content.getProperty(DmConstants.KEY_USER);
    String site = content.getProperty(DmConstants.KEY_SITE);
    String path = content.getProperty(DmConstants.KEY_PATH);
    String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
    String contentType = content.getProperty(DmConstants.KEY_CONTENT_TYPE);
    InputStream input = content.getContentStream();
    boolean isPreview = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_IS_PREVIEW));
    boolean createFolders = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_CREATE_FOLDERS));
    String unlockValue = content.getProperty(DmConstants.KEY_UNLOCK);
    boolean unlock = (!StringUtils.isEmpty(unlockValue) && unlockValue.equalsIgnoreCase("false")) ? false : true;
    String parentContentPath = path;
    if (parentContentPath.endsWith(FILE_SEPARATOR + fileName)) {
        parentContentPath = parentContentPath.replace(FILE_SEPARATOR + fileName, "");
    } else {
        path = path + FILE_SEPARATOR + fileName;
    }
    try {
        // look up the path content first
        ContentItemTO parentItem = contentService.getContentItem(site, parentContentPath, 0);
        boolean parentContentExists = contentService.contentExists(site, parentContentPath);
        if (!parentContentExists && createFolders) {
            parentItem = createMissingFoldersInPath(site, path, isPreview);
        }
        if (parentItem != null) {
            // look up the path content first
            if (parentItem.getName().equals(fileName)) {
                ContentItemTO item = contentService.getContentItem(site, path, 0);
                InputStream existingContent = contentService.getContent(site, path);
                updateFile(site, item, path, input, user, isPreview, unlock, result);
                content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_UPDATE);
                if (unlock) {
                    // TODO: We need ability to lock/unlock content in repo
                    contentService.unLockContent(site, path);
                    logger.debug("Unlocked the content " + parentContentPath);
                }
                return;
            } else {
                // otherwise, create new one
                if (path.endsWith(DmConstants.XML_PATTERN) && !path.endsWith(DmConstants.INDEX_FILE)) {
                    parentContentPath = path.substring(0, path.lastIndexOf(FILE_SEPARATOR));
                    parentItem = contentService.getContentItem(site, parentContentPath, 0);
                }
                boolean fileExists = contentService.contentExists(site, path);
                if (fileExists) {
                    ContentItemTO contentItem = contentService.getContentItem(site, path, 0);
                    InputStream existingContent = contentService.getContent(site, path);
                    updateFile(site, contentItem, path, input, user, isPreview, unlock, result);
                    content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_UPDATE);
                    if (unlock) {
                        // TODO: We need ability to lock/unlock content in repo
                        contentService.unLockContent(site, path);
                        logger.debug("Unlocked the content site: " + site + " path: " + path);
                    }
                    return;
                } else {
                    ContentItemTO newFileItem = createNewFile(site, parentItem, fileName, contentType, input, user, unlock, result);
                    content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_CREATE);
                    return;
                }
            }
        } else {
            throw new ContentNotFoundException(path + " does not exist in site: " + site);
        }
    } catch (ContentNotFoundException | RepositoryLockedException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Error: ", e);
        throw new ContentNotFoundException("Unexpected exception ", e);
    } finally {
        ContentUtils.release(input);
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) InputStream(java.io.InputStream) ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException)

Example 12 with ResultTO

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

the class ProcessContentExecutorImpl method processContent.

@Override
public ResultTO processContent(String id, InputStream input, boolean isXml, Map<String, String> params, String chainName) throws ServiceLayerException {
    final ContentProcessorPipeline chain = processorChains.get(chainName);
    try {
        if (chain != null) {
            if (StringUtils.isEmpty(params.get(DmConstants.KEY_USER))) {
                String user = securityService.getCurrentUser();
                params.put(DmConstants.KEY_USER, user);
            }
            final ResultTO result = new ResultTO();
            try {
                final PipelineContent content = new PipelineContentImpl(id, input, isXml, null, StudioConstants.CONTENT_ENCODING, params);
                chain.processContent(content, result);
            } catch (ContentProcessException e) {
                logger.error("Error in chain for write content", e);
                throw e;
            } catch (RuntimeException e) {
                logger.error("Error in chain for write content", e);
                throw e;
            } finally {
                ContentUtils.release(input);
            }
            return result;
        } else {
            ContentUtils.release(input);
            throw new ServiceLayerException(chainName + " is not defined.");
        }
    } finally {
        String s = params.get(DmConstants.KEY_USER);
    // AuthenticationUtil.setFullyAuthenticatedUser(s);
    }
}
Also used : ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) PipelineContentImpl(org.craftercms.studio.impl.v1.content.pipeline.PipelineContentImpl) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PipelineContent(org.craftercms.studio.api.v1.content.pipeline.PipelineContent) ContentProcessorPipeline(org.craftercms.studio.api.v1.content.pipeline.ContentProcessorPipeline) ResultTO(org.craftercms.studio.api.v1.to.ResultTO)

Example 13 with ResultTO

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

the class ExtractAssetDependencyProcessor method process.

public void process(PipelineContent content, ResultTO result) throws ContentProcessException {
    String site = content.getProperty(DmConstants.KEY_SITE);
    String folderPath = content.getProperty(DmConstants.KEY_FOLDER_PATH);
    String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
    String path = (folderPath.endsWith(FILE_SEPARATOR)) ? folderPath + fileName : folderPath + FILE_SEPARATOR + fileName;
    try {
        dependencyService.upsertDependencies(site, path);
    } catch (ServiceLayerException e) {
        throw new ContentProcessException(e);
    }
}
Also used : ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 14 with ResultTO

use of org.craftercms.studio.api.v1.to.ResultTO 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 15 with ResultTO

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

the class WorkflowServiceImpl method reject.

@Override
@SuppressWarnings("unchecked")
@ValidateParams
public ResultTO reject(@ValidateStringParam(name = "site") String site, String request) throws ServiceLayerException {
    ResultTO result = new ResultTO();
    try {
        String approver = securityService.getCurrentUser();
        JSONObject requestObject = JSONObject.fromObject(request);
        String reason = (requestObject.containsKey(JSON_KEY_REASON)) ? requestObject.getString(JSON_KEY_REASON) : "";
        JSONArray items = requestObject.getJSONArray(JSON_KEY_ITEMS);
        String scheduledDate = null;
        if (requestObject.containsKey(JSON_KEY_SCHEDULED_DATE)) {
            scheduledDate = requestObject.getString(JSON_KEY_SCHEDULED_DATE);
        }
        int length = items.size();
        if (length > 0) {
            SimpleDateFormat format = new SimpleDateFormat(StudioConstants.DATE_PATTERN_WORKFLOW_WITH_TZ);
            List<DmDependencyTO> submittedItems = new ArrayList<DmDependencyTO>();
            for (int index = 0; index < length; index++) {
                String stringItem = items.optString(index);
                // JSONObject item = items.getJSONObject(index);
                // getSubmittedItem(site, item, format, scheduledDate);
                DmDependencyTO submittedItem = null;
                submittedItem = getSubmittedItem(site, stringItem, format, scheduledDate, null);
                submittedItems.add(submittedItem);
            }
            List<String> paths = new ArrayList<String>();
            List<AuditLogParameter> auditLogParameters = new ArrayList<AuditLogParameter>();
            for (DmDependencyTO goLiveItem : submittedItems) {
                if (contentService.contentExists(site, goLiveItem.getUri())) {
                    paths.add(goLiveItem.getUri());
                }
                AuditLogParameter auditLogParameter = new AuditLogParameter();
                auditLogParameter.setTargetId(site + ":" + goLiveItem.getUri());
                auditLogParameter.setTargetType(TARGET_TYPE_CONTENT_ITEM);
                auditLogParameter.setTargetValue(goLiveItem.getUri());
                auditLogParameters.add(auditLogParameter);
            }
            objectStateService.setSystemProcessingBulk(site, paths, true);
            Set<String> cancelPaths = new HashSet<String>();
            cancelPaths.addAll(paths);
            deploymentService.cancelWorkflowBulk(site, cancelPaths);
            reject(site, submittedItems, reason, approver);
            SiteFeed siteFeed = siteService.getSite(site);
            AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
            auditLog.setOperation(OPERATION_REJECT);
            auditLog.setActorId(approver);
            auditLog.setSiteId(siteFeed.getId());
            auditLog.setPrimaryTargetId(site);
            auditLog.setPrimaryTargetType(TARGET_TYPE_SITE);
            auditLog.setPrimaryTargetValue(site);
            auditLog.setParameters(auditLogParameters);
            auditServiceInternal.insertAuditLog(auditLog);
            objectStateService.setSystemProcessingBulk(site, paths, false);
            result.setSuccess(true);
            result.setStatus(200);
            result.setMessage(notificationService.getNotificationMessage(site, NotificationMessageType.CompleteMessages, NotificationService.COMPLETE_REJECT, Locale.ENGLISH));
        } else {
            result.setSuccess(false);
            result.setMessage("No items provided for preparation.");
        }
    } catch (JSONException | DeploymentException e) {
        result.setSuccess(false);
        result.setMessage(e.getMessage());
    }
    return result;
}
Also used : JSONArray(net.sf.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(net.sf.json.JSONException) DmDependencyTO(org.craftercms.studio.api.v1.to.DmDependencyTO) ResultTO(org.craftercms.studio.api.v1.to.ResultTO) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) JSONObject(net.sf.json.JSONObject) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) AuditLogParameter(org.craftercms.studio.api.v2.dal.AuditLogParameter) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Aggregations

ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)12 ResultTO (org.craftercms.studio.api.v1.to.ResultTO)8 ContentProcessException (org.craftercms.studio.api.v1.exception.ContentProcessException)6 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)6 SimpleDateFormat (java.text.SimpleDateFormat)5 ArrayList (java.util.ArrayList)5 JSONArray (net.sf.json.JSONArray)5 JSONException (net.sf.json.JSONException)5 JSONObject (net.sf.json.JSONObject)5 ContentAssetInfoTO (org.craftercms.studio.api.v1.to.ContentAssetInfoTO)5 DmDependencyTO (org.craftercms.studio.api.v1.to.DmDependencyTO)5 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)4 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)4 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)4 HashSet (java.util.HashSet)3 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)3 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)3 MultiChannelPublishingContext (org.craftercms.studio.api.v1.service.workflow.context.MultiChannelPublishingContext)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2