Search in sources :

Example 36 with ContentItemTO

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

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

the class FormDmContentProcessor method fileToFolder.

@Override
public String fileToFolder(String site, String path) throws SiteNotFoundException {
    if (contentService.contentExists(site, path)) {
        ContentItemTO itemTO = contentService.getContentItem(site, path, 0);
        if (itemTO.isFolder() || itemTO.isDeleted()) {
            return path;
        }
        int index = path.lastIndexOf(FILE_SEPARATOR);
        String folderPath = path.substring(0, index);
        String parentFileName = itemTO.getName();
        int dotIndex = parentFileName.indexOf(".");
        String folderName = (dotIndex > 0) ? parentFileName.substring(0, parentFileName.indexOf(".")) : parentFileName;
        contentService.createFolder(site, folderPath, folderName);
        folderPath = folderPath + FILE_SEPARATOR + folderName;
        contentService.moveContent(site, path, folderPath + FILE_SEPARATOR + DmConstants.INDEX_FILE);
        logger.debug("Changed file to folder from " + path + " to " + folderPath);
        return folderPath;
    } else {
        return path;
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO)

Example 38 with ContentItemTO

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

the class WorkflowServiceImpl method getCategoryItems.

/**
 * get the top category items that to be displayed in UI
 *
 * @param site
 */
protected List<ContentItemTO> getCategoryItems(final String site) {
    String siteRootPrefix = servicesConfig.getRootPrefix(site);
    List<ContentItemTO> categories = new ArrayList<>();
    List<DmFolderConfigTO> folders = servicesConfig.getFolders(site);
    for (DmFolderConfigTO folder : folders) {
        String uri = (folder.isAttachRootPrefix()) ? siteRootPrefix + folder.getPath() : folder.getPath();
        // child folders and add them as categories
        if (folder.isReadDirectChildren()) {
            ContentItemTO rootItem = contentService.getContentItemTree(site, siteRootPrefix + folder.getPath(), 1);
            if (rootItem != null) {
                if (rootItem.children != null) {
                    for (ContentItemTO childItem : rootItem.children) {
                        categories.add(childItem);
                    }
                }
                categories.add(rootItem);
            }
        } else {
            ContentItemTO categoryItem = new ContentItemTO();
            String timeZone = servicesConfig.getDefaultTimezone(site);
            categoryItem.setTimezone(timeZone);
            categoryItem.setName(folder.getName());
            categoryItem.setInternalName(folder.getName());
            categoryItem.setUri(uri);
            categoryItem.setPath(uri);
            categoryItem.setCategoryRoot(uri);
            categories.add(categoryItem);
        }
    }
    return categories;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) DmFolderConfigTO(org.craftercms.studio.api.v1.to.DmFolderConfigTO) ArrayList(java.util.ArrayList)

Example 39 with ContentItemTO

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

the class WorkflowServiceImpl method doSubmit.

protected void doSubmit(final String site, final DmDependencyTO dependencyTO, final ZonedDateTime scheduledDate, final boolean sendEmail, final boolean submitForDeletion, final String user, final boolean notifyAdmin, final String submissionComment, String environment) throws ServiceLayerException {
    // first remove from workflow
    removeFromWorkflow(site, dependencyTO.getUri(), true);
    ContentItemTO item = contentService.getContentItem(site, dependencyTO.getUri());
    Map<String, Object> properties = new HashMap<>();
    properties.put(ItemMetadata.PROP_SUBMITTED_BY, user);
    properties.put(ItemMetadata.PROP_SEND_EMAIL, sendEmail ? 1 : 0);
    properties.put(ItemMetadata.PROP_SUBMITTED_FOR_DELETION, submitForDeletion ? 1 : 0);
    properties.put(ItemMetadata.PROP_SUBMISSION_COMMENT, submissionComment);
    properties.put(ItemMetadata.PROP_SUBMITTED_TO_ENVIRONMENT, environment);
    if (null == scheduledDate) {
        properties.put(ItemMetadata.PROP_LAUNCH_DATE, null);
    } else {
        properties.put(ItemMetadata.PROP_LAUNCH_DATE, scheduledDate);
    }
    if (!objectMetadataManager.metadataExist(site, dependencyTO.getUri())) {
        objectMetadataManager.insertNewObjectMetadata(site, dependencyTO.getUri());
    }
    objectMetadataManager.setObjectMetadata(site, dependencyTO.getUri(), properties);
    if (scheduledDate != null) {
        objectStateService.transition(site, item, TransitionEvent.SUBMIT_WITH_WORKFLOW_SCHEDULED);
    } else {
        objectStateService.transition(site, item, TransitionEvent.SUBMIT_WITH_WORKFLOW_UNSCHEDULED);
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) HashMap(java.util.HashMap) JSONObject(net.sf.json.JSONObject)

Example 40 with ContentItemTO

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

the class WorkflowServiceImpl method fillQueue.

@Override
@ValidateParams
public void fillQueue(@ValidateStringParam(name = "site") String site, GoLiveQueue goLiveQueue, GoLiveQueue inProcessQueue) throws ServiceLayerException {
    List<ItemState> changeSet = objectStateService.getSubmittedItems(site);
    // regular categories specified in the configuration
    if (changeSet != null) {
        // add all content items from each task if task is the review task
        for (ItemState state : changeSet) {
            try {
                if (contentService.contentExists(state.getSite(), state.getPath())) {
                    ContentItemTO item = contentService.getContentItem(state.getSite(), state.getPath(), 0);
                    Set<String> permissions = securityService.getUserPermissions(site, item.getUri(), securityService.getCurrentUser(), Collections.<String>emptyList());
                    if (permissions.contains(StudioConstants.PERMISSION_VALUE_PUBLISH)) {
                        addToQueue(site, goLiveQueue, inProcessQueue, item, state);
                    }
                } else {
                    _cancelWorkflow(site, state.getPath());
                    objectStateService.deleteObjectStateForPath(site, state.getPath());
                    objectMetadataManager.deleteObjectMetadata(site, state.getPath());
                }
            } catch (Exception e) {
                logger.error("Could not warm cache for [" + state.getSite() + " : " + state.getPath() + "] " + e.getMessage());
            }
        }
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ItemState(org.craftercms.studio.api.v1.dal.ItemState) 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) 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