Search in sources :

Example 61 with ContentItemTO

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

the class ContentServiceImpl method unLockContent.

@Override
@ValidateParams
public void unLockContent(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path) {
    ContentItemTO item = getContentItem(site, path, 0);
    // this unlocks too
    objectStateService.transition(site, item, TransitionEvent.CANCEL_EDIT);
    _contentRepository.unLockItem(site, path);
    objectMetadataManager.unLockContent(site, path);
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 62 with ContentItemTO

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

the class ContentServiceImpl method childDeleteItems.

/**
 * Iterate over all paths inside the folder
 */
protected void childDeleteItems(String site, ContentItemTO contentItem, GoLiveDeleteCandidates items) throws ServiceLayerException {
    if (contentItem.isFolder()) {
        contentItem = getContentItemTree(site, contentItem.getUri(), 1);
        if (contentItem.getChildren() != null && contentItem.getNumOfChildren() > 0) {
            for (ContentItemTO child : contentItem.getChildren()) {
                childDeleteItems(site, child, items);
            }
        }
    } else {
        addDependenciesToDelete(site, contentItem.getUri(), contentItem.getUri(), items);
        addRemovedDependenicesToDelete(site, contentItem.getUri(), items);
    }
    // add the child path
    items.getPaths().add(contentItem.getUri());
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO)

Example 63 with ContentItemTO

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

the class ContentServiceImpl method copyContent.

/**
 * internal method copy that handles
 * Get dependencies is already recursive
 */
protected String copyContent(String site, String fromPath, String toPath, Set<String> processedPaths) {
    String retNewFileName = null;
    String lifecycleOp = DmContentLifeCycleService.ContentLifeCycleOperation.COPY.toString();
    String user = securityService.getCurrentUser();
    String copyPath = null;
    try {
        Map<String, String> copyPathMap = constructNewPathforCutCopy(site, fromPath, toPath, true);
        copyPath = copyPathMap.get("FILE_PATH");
        String copyPathModifier = copyPathMap.get("MODIFIER");
        String copyPathFileName = copyPathMap.get("FILE_NAME");
        String copyPathFolder = copyPathMap.get("FILE_FOLDER");
        String copyPathOnly = copyPath.substring(0, copyPath.lastIndexOf(FILE_SEPARATOR));
        String copyFileName = copyPath.substring(copyPath.lastIndexOf(FILE_SEPARATOR) + 1);
        if (!processedPaths.contains(copyPath)) {
            ContentItemTO fromItem = getContentItem(site, fromPath, 0);
            if (fromItem.isFolder()) {
                createFolder(site, copyPathOnly, copyFileName);
                // copy was successful, return the new name
                retNewFileName = copyPath;
            } else {
                InputStream copyContent = null;
                try {
                    String contentType = fromItem.getContentType();
                    InputStream fromContent = getContent(site, fromPath);
                    if (fromPath.endsWith(DmConstants.XML_PATTERN)) {
                        Document fromDocument = ContentUtils.convertStreamToXml(fromContent);
                        Map<String, String> fromPageIds = getContentIds(fromDocument);
                        logger.debug("copying file for site {0} from {1} to {2}, new name is {3}", site, fromPath, toPath, copyPath);
                        // come up with a new object ID and group ID for the object
                        Map<String, String> copyObjectIds = contentItemIdGenerator.getIds();
                        Map<String, String> copyDependencies = getCopyDependencies(site, fromPath, fromPath);
                        copyDependencies = getItemSpecificDependencies(site, fromPath, fromDocument, copyDependencies);
                        logger.debug("Calculated copy dependencies: {0}, {1}", fromPath, copyDependencies);
                        // Duplicate the children
                        for (String dependencyKey : copyDependencies.keySet()) {
                            String dependencyPath = copyDependencies.get(dependencyKey);
                            String copyDepPath = dependencyPath;
                            // try a simple substitution
                            copyDepPath = copyDepPath.replaceAll(fromPageIds.get(KEY_PAGE_ID), copyObjectIds.get(KEY_PAGE_ID));
                            copyDepPath = copyDepPath.replaceAll(fromPageIds.get(KEY_PAGE_GROUP_ID), copyObjectIds.get(KEY_PAGE_GROUP_ID));
                            ContentItemTO targetPathItem = getContentItem(site, copyDepPath);
                            if (targetPathItem != null && targetPathItem.isFolder()) {
                                copyDepPath = copyDepPath + FILE_SEPARATOR + FilenameUtils.getName(dependencyKey);
                                copyDepPath = copyDepPath.replaceAll(FILE_SEPARATOR + FILE_SEPARATOR, FILE_SEPARATOR);
                            } else if (!copyDepPath.endsWith(DmConstants.XML_PATTERN)) {
                                copyDepPath = ContentUtils.getParentUrl(copyDepPath);
                            }
                            logger.debug("Translated dependency path from {0} to {1}", dependencyPath, copyDepPath);
                            String newCopyDepthPath = copyContent(site, dependencyKey, copyDepPath, processedPaths);
                            fromDocument = replaceCopyDependency(fromDocument, dependencyKey, newCopyDepthPath);
                        }
                        // update the file name / folder values
                        Document copyDocument = updateContentOnCopy(fromDocument, copyPathFileName, copyPathFolder, copyObjectIds, copyPathModifier);
                        copyContent = ContentUtils.convertDocumentToStream(copyDocument, CONTENT_ENCODING);
                    }
                    // This code is very similar to what is in writeContent. Consolidate this code?
                    Map<String, String> params = new HashMap<String, String>();
                    params.put(DmConstants.KEY_SITE, site);
                    params.put(DmConstants.KEY_PATH, copyPathOnly);
                    params.put(DmConstants.KEY_FILE_NAME, copyFileName);
                    params.put(DmConstants.KEY_USER, user);
                    params.put(DmConstants.KEY_CONTENT_TYPE, contentType);
                    params.put(DmConstants.KEY_CREATE_FOLDERS, "true");
                    params.put(DmConstants.KEY_EDIT, "true");
                    params.put(DmConstants.KEY_ACTIVITY_TYPE, "false");
                    params.put(DmConstants.KEY_SKIP_CLEAN_PREVIEW, "true");
                    params.put(DmConstants.KEY_COPIED_CONTENT, "true");
                    params.put(DmConstants.CONTENT_LIFECYCLE_OPERATION, lifecycleOp);
                    String id = site + ":" + copyPathOnly + ":" + copyFileName + ":" + contentType;
                    // processContent will close the input stream
                    if (copyFileName.endsWith(DmConstants.XML_PATTERN)) {
                        processContent(id, copyContent, true, params, DmConstants.CONTENT_CHAIN_FORM);
                    } else {
                        processContent(id, fromContent, false, params, DmConstants.CONTENT_CHAIN_ASSET);
                    }
                    ItemState itemState = objectStateService.getObjectState(site, copyPath);
                    if (itemState == null) {
                        ContentItemTO copyItem = getContentItem(site, copyPath, 0);
                        objectStateService.insertNewEntry(site, copyItem);
                        objectStateService.setSystemProcessing(site, copyPath, false);
                    }
                    // copy was successful, return the new name
                    retNewFileName = copyPath;
                    // track that we already copied so we don't follow a circular dependency
                    processedPaths.add(copyPath);
                } catch (ContentNotFoundException eContentNotFound) {
                    logger.debug("Content not found while copying content for site {0} from {1} to {2}," + " new name is {3}", eContentNotFound, site, fromPath, toPath, copyPath);
                } catch (DocumentException eParseException) {
                    logger.error("General Error while copying content for site {0} from {1} to {2}," + " new name is {3}", eParseException, site, fromPath, toPath, copyPath);
                } catch (CryptoException e) {
                    logger.error("Unexpected Error while copying content for site {0} from {1} to {2}," + " new name is {3}", e, site, fromPath, toPath, copyPath);
                } finally {
                    IOUtils.closeQuietly(copyContent);
                }
            }
        } else {
            // no need to process
            retNewFileName = copyPath;
        }
    } catch (ServiceLayerException eServiceLayerException) {
        logger.info("General Error while copying content for site {0} from {1} to {2}, new name is {3}", eServiceLayerException, site, fromPath, toPath, copyPath);
    }
    return retNewFileName;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) HashMap(java.util.HashMap) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) InputStream(java.io.InputStream) ItemState(org.craftercms.studio.api.v1.dal.ItemState) DocumentException(org.dom4j.DocumentException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) Document(org.dom4j.Document) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 64 with ContentItemTO

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

the class ContentServiceImpl method generateDeleteActivity.

protected void generateDeleteActivity(String site, String path, String approver) throws SiteNotFoundException {
    // TODO: SJ: activities. Fix in 3.1+ by introducing the audit service and refactoring accordingly
    if (StringUtils.isEmpty(approver)) {
        approver = securityService.getCurrentUser();
    }
    boolean exists = contentExists(site, path);
    if (exists) {
        ContentItemTO item = getContentItem(site, path, 0);
        ItemMetadata properties = objectMetadataManager.getProperties(site, path);
        String user = (properties != null && !StringUtils.isEmpty(properties.getSubmittedBy()) ? properties.getSubmittedBy() : approver);
        Map<String, String> extraInfo = new HashMap<String, String>();
        if (item.isFolder()) {
            extraInfo.put(DmConstants.KEY_CONTENT_TYPE, CONTENT_TYPE_FOLDER);
        } else {
            extraInfo.put(DmConstants.KEY_CONTENT_TYPE, getContentTypeClass(site, path));
        }
        logger.debug("[DELETE] posting delete activity on " + path + " by " + user + " in " + site);
        SiteFeed siteFeed = siteService.getSite(site);
        AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
        auditLog.setOperation(OPERATION_DELETE);
        auditLog.setSiteId(siteFeed.getId());
        auditLog.setActorId(user);
        auditLog.setPrimaryTargetId(site + ":" + path);
        auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
        auditLog.setPrimaryTargetValue(path);
        auditLog.setPrimaryTargetSubtype(getContentTypeClass(site, path));
        auditServiceInternal.insertAuditLog(auditLog);
        // process content life cycle
        if (path.endsWith(DmConstants.XML_PATTERN)) {
            String contentType = item.getContentType();
            dmContentLifeCycleService.process(site, user, path, contentType, DmContentLifeCycleService.ContentLifeCycleOperation.DELETE, null);
        }
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) HashMap(java.util.HashMap) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) ItemMetadata(org.craftercms.studio.api.v1.dal.ItemMetadata)

Example 65 with ContentItemTO

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

the class ContentServiceImpl method getContentItem.

@Override
@ValidateParams
public ContentItemTO getContentItem(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, @ValidateIntegerParam(name = "depth") int depth) {
    ContentItemTO item = null;
    logger.debug("Getting content item for site '{}' path '{}' depth '{}'", site, path, depth);
    DebugUtils.addDebugStack(logger);
    long startTime = System.currentTimeMillis();
    try {
        if (contentExists(site, path)) {
            // get item from cache
            item = loadContentItem(site, path);
            if (depth != 0) {
                item = populateItemChildren(item, depth);
            }
            // POPULATE LOCK STATUS
            populateMetadata(site, item);
            // POPULATE WORKFLOW STATUS
            if (!item.isFolder() || item.isContainer()) {
                populateWorkflowProperties(site, item);
            } else {
                item.setNew(!objectStateService.isFolderLive(site, item.getUri()));
                item.isNew = item.isNew();
            }
        } else {
            item = createDummyDmContentItemForDeletedNode(site, path);
        }
    } catch (Exception err) {
        logger.debug("error constructing item for object at site '{}' path '{}'", err, site, path);
    }
    long executionTime = System.currentTimeMillis() - startTime;
    logger.debug("Content item from site '{}' path '{}' retrieved in '{}' milli-seconds", site, path, executionTime);
    return item;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) AuthenticationException(org.craftercms.studio.api.v1.exception.security.AuthenticationException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) SAXException(org.xml.sax.SAXException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) DocumentException(org.dom4j.DocumentException) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) 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