Search in sources :

Example 6 with ContentItemTO

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

the class ContentServiceImpl method populateContentDrivenProperties.

@SuppressWarnings("unchecked")
protected ContentItemTO populateContentDrivenProperties(String site, ContentItemTO item) throws Exception {
    // This method load an XML content item and populates properties in the TO from the XML
    // TODO: SJ: Two problems here that need to be fixed in 3.1+
    // TODO: SJ: Use Crafter Core for some/all of this work
    // TODO: SJ: Much of this seems hardcoded and must be extensible/configurable via key:xpath in config
    String contentPath = item.uri;
    logger.debug("Populating page props '{}'", contentPath);
    item.setLevelDescriptor(item.name.equals(servicesConfig.getLevelDescriptorName(site)));
    item.page = ContentUtils.matchesPatterns(item.getUri(), servicesConfig.getPagePatterns(site));
    item.isPage = item.page;
    // TODO: SJ: This and item below are duplicated due to UI issues
    item.previewable = item.page;
    // TODO: SJ: Fix this in 3.1+
    item.isPreviewable = item.previewable;
    item.component = ContentUtils.matchesPatterns(item.getUri(), servicesConfig.getComponentPatterns(site)) || item.isLevelDescriptor();
    item.isComponent = item.component;
    item.asset = ContentUtils.matchesPatterns(item.getUri(), servicesConfig.getAssetPatterns(site));
    item.isAsset = item.asset;
    item.document = ContentUtils.matchesPatterns(item.getUri(), servicesConfig.getDocumentPatterns(site));
    item.isDocument = item.document;
    item.uri = contentPath;
    // TODO: SJ: This is hokey, fix in 3.1+
    item.path = contentPath.substring(0, contentPath.lastIndexOf(FILE_SEPARATOR));
    // TODO: SJ: This is hokey, fix in 3.1+
    item.name = contentPath.substring(contentPath.lastIndexOf(FILE_SEPARATOR) + 1);
    item.browserUri = contentPath;
    if (item.page) {
        // TODO: SJ: This is hokey, fix in 4.x
        item.browserUri = contentPath.replace(FILE_SEPARATOR + "site" + FILE_SEPARATOR + "website", "").replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, "");
    }
    Document contentDoc = this.getContentAsDocument(site, contentPath);
    if (contentDoc != null) {
        Element rootElement = contentDoc.getRootElement();
        String internalName = rootElement.valueOf("internal-name");
        String contentType = rootElement.valueOf("content-type");
        String disabled = rootElement.valueOf("disabled");
        String savedAsDraft = rootElement.valueOf("savedAsDraft");
        String navigation = rootElement.valueOf("placeInNav");
        String hideInAuthoring = rootElement.valueOf("hideInAuthoring");
        String displayTemplate = rootElement.valueOf("display-template");
        item.internalName = (internalName != null) ? internalName : null;
        item.contentType = (contentType != null) ? contentType : null;
        item.disabled = (disabled != null && "true".equalsIgnoreCase(disabled)) ? true : false;
        item.savedAsDraft = (savedAsDraft != null && "true".equalsIgnoreCase(savedAsDraft)) ? true : false;
        item.hideInAuthoring = (hideInAuthoring != null && "true".equalsIgnoreCase(hideInAuthoring)) ? true : false;
        item.navigation = (navigation != null && "true".equalsIgnoreCase(navigation)) ? true : false;
        item.floating = !item.navigation;
        item.setOrders(getItemOrders(rootElement.selectNodes("//" + DmXmlConstants.ELM_ORDER_DEFAULT)));
        if (displayTemplate != null) {
            RenderingTemplateTO template = new RenderingTemplateTO();
            template.uri = displayTemplate;
            // FIXME: SJ: 3.1+
            template.name = "DEFAULT";
            item.renderingTemplates.add(template);
        }
    } else {
        logger.debug("no xml document could be loaded for site '{}' path '{}'", site, contentPath);
    }
    Pattern taxonomyPattern = Pattern.compile(CONTENT_TYPE_TAXONOMY_REGEX);
    Matcher matcher = taxonomyPattern.matcher(contentPath);
    if (matcher.matches()) {
        item.contentType = CONTENT_TYPE_TAXONOMY;
    }
    return item;
}
Also used : RenderingTemplateTO(org.craftercms.studio.api.v1.to.RenderingTemplateTO) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Element(org.dom4j.Element) Document(org.dom4j.Document)

Example 7 with ContentItemTO

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

the class ContentServiceImpl method getDeleteCandidates.

@Override
@ValidateParams
public GoLiveDeleteCandidates getDeleteCandidates(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "relativePath") String relativePath) throws ServiceLayerException {
    ContentItemTO contentItem = getContentItem(site, relativePath);
    GoLiveDeleteCandidates deletedItems = new GoLiveDeleteCandidates(site, this, objectStateService);
    if (contentItem != null) {
        childDeleteItems(site, contentItem, deletedItems);
    // update summary for all uri's delete
    }
    return deletedItems;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) GoLiveDeleteCandidates(org.craftercms.studio.api.v1.to.GoLiveDeleteCandidates) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 8 with ContentItemTO

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

the class ContentServiceImpl method createDummyDmContentItemForDeletedNode.

@Override
@ValidateParams
public ContentItemTO createDummyDmContentItemForDeletedNode(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "relativePath") String relativePath) {
    // TODO: SJ: Think of another way to do this in 3.1+
    ContentItemTO item = new ContentItemTO();
    String timeZone = servicesConfig.getDefaultTimezone(site);
    item.timezone = timeZone;
    String name = ContentUtils.getPageName(relativePath);
    String folderPath = (name.equals(DmConstants.INDEX_FILE)) ? relativePath.replace(FILE_SEPARATOR + name, "") : relativePath;
    item.path = folderPath;
    /**
     * Internal name should be just folder name
     */
    String internalName = folderPath;
    int index = folderPath.lastIndexOf(FILE_SEPARATOR);
    if (index != -1)
        internalName = folderPath.substring(index + 1);
    item.internalName = internalName;
    item.isDisabled = false;
    item.isNavigation = false;
    item.name = name;
    item.uri = relativePath;
    // set content type based on the relative Path
    String contentTypeClass = getContentTypeClass(site, relativePath);
    item.contentType = contentTypeClass;
    if (contentTypeClass.equals(CONTENT_TYPE_COMPONENT)) {
        item.component = true;
    } else if (contentTypeClass.equals(CONTENT_TYPE_DOCUMENT)) {
        item.document = true;
    }
    // set if the content is new
    item.isDeleted = true;
    item.deleted = true;
    item.isContainer = false;
    item.container = false;
    item.isNew = false;
    item.isInProgress = false;
    item.timezone = servicesConfig.getDefaultTimezone(site);
    item.isPreviewable = false;
    item.browserUri = getBrowserUri(item);
    return item;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 9 with ContentItemTO

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

the class ContentServiceImpl method updateDatabaseOnMove.

protected void updateDatabaseOnMove(String site, String fromPath, String movePath) throws SiteNotFoundException {
    logger.debug("updateDatabaseOnMove FROM {0} TO {1}  ", fromPath, movePath);
    String user = securityService.getCurrentUser();
    Map<String, String> params = new HashMap<>();
    params.put(DmConstants.KEY_SOURCE_PATH, fromPath);
    params.put(DmConstants.KEY_TARGET_PATH, movePath);
    // These do not exist in 3.0, note some extensions may be using it
    // params.put(DmConstants.KEY_SOURCE_FULL_PATH, expandRelativeSitePath(site, fromPath));
    // params.put(DmConstants.KEY_TARGET_FULL_PATH, expandRelativeSitePath(site, movePath));
    ContentItemTO renamedItem = getContentItem(site, movePath, 0);
    String contentType = renamedItem.getContentType();
    if (!renamedItem.isFolder()) {
        dmContentLifeCycleService.process(site, user, movePath, contentType, DmContentLifeCycleService.ContentLifeCycleOperation.RENAME, params);
        // change the path of this object in the object state database
        objectStateService.updateObjectPath(site, fromPath, movePath);
        objectStateService.transition(site, renamedItem, SAVE);
        renamedItem = getContentItem(site, movePath, 0);
    }
    // update metadata
    if (!objectMetadataManager.isRenamed(site, fromPath)) {
        // if an item was previously moved, we do not track intermediate moves because it will
        // ultimately orphan deployed content.  Old Path is always the OLDEST DEPLOYED PATH
        ItemMetadata metadata = objectMetadataManager.getProperties(site, fromPath);
        if (metadata == null && !renamedItem.isFolder()) {
            if (!objectMetadataManager.metadataExist(site, fromPath)) {
                objectMetadataManager.insertNewObjectMetadata(site, fromPath);
            }
            metadata = objectMetadataManager.getProperties(site, fromPath);
        }
        if (!renamedItem.isNew() && !renamedItem.isFolder()) {
            // if the item is not new, we need to track the old URL for deployment
            logger.debug("item is not new, and has not previously been moved. Track the old URL {0}", fromPath);
            Map<String, Object> objMetadataProps = new HashMap<String, Object>();
            objMetadataProps.put(ItemMetadata.PROP_RENAMED, 1);
            objMetadataProps.put(ItemMetadata.PROP_OLD_URL, fromPath);
            objectMetadataManager.setObjectMetadata(site, fromPath, objMetadataProps);
        }
    }
    if (!renamedItem.isFolder()) {
        if (objectMetadataManager.metadataExist(site, movePath)) {
            if (!StringUtils.equalsIgnoreCase(fromPath, movePath)) {
                objectMetadataManager.deleteObjectMetadata(site, movePath);
            }
        }
        objectMetadataManager.updateObjectPath(site, fromPath, movePath);
    }
    // write activity stream
    SiteFeed siteFeed = siteService.getSite(site);
    AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
    auditLog.setOperation(OPERATION_MOVE);
    auditLog.setSiteId(siteFeed.getId());
    auditLog.setActorId(user);
    auditLog.setPrimaryTargetId(site + ":" + movePath);
    if (renamedItem.isFolder()) {
        auditLog.setPrimaryTargetType(TARGET_TYPE_FOLDER);
    } else {
        auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
    }
    auditLog.setPrimaryTargetValue(movePath);
    auditLog.setPrimaryTargetSubtype(getContentTypeClass(site, movePath));
    auditServiceInternal.insertAuditLog(auditLog);
    updateDependenciesOnMove(site, fromPath, movePath);
}
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 10 with ContentItemTO

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

the class ContentServiceImpl method getNextAvailableName.

@Override
@ValidateParams
public String getNextAvailableName(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path) {
    // TODO: SJ: Refactor to be faster, and make it work regardless (seems to fail above 10) in 3.1+
    String[] levels = path.split(FILE_SEPARATOR);
    int length = levels.length;
    if (length > 0) {
        ContentItemTO item = getContentItem(site, path, 0);
        if (item != null) {
            String name = ContentUtils.getPageName(path);
            String parentPath = ContentUtils.getParentUrl(path);
            ContentItemTO parentItem = getContentItemTree(site, parentPath, 1);
            if (parentItem != null) {
                int lastIndex = name.lastIndexOf(".");
                String ext = (item.isFolder()) ? "" : name.substring(lastIndex);
                String originalName = (item.isFolder() || item.isContainer()) ? name : name.substring(0, lastIndex);
                List<ContentItemTO> children = parentItem.getChildren();
                // pattern matching doesn't work here
                // String childNamePattern = originalName + "%" + ext;
                int lastNumber = 0;
                String namePattern = originalName + "-[0-9]+" + ext;
                if (children != null && children.size() > 0) {
                    // since it is already sorted, we only care about the last matching item
                    for (ContentItemTO child : children) {
                        if (((item.isFolder() || item.isContainer()) == (child.isFolder() || child.isContainer()))) {
                            String childName = child.getName();
                            if ((child.isFolder() || child.isContainer())) {
                                childName = ContentUtils.getPageName(child.getBrowserUri());
                            }
                            if (childName.matches(namePattern)) {
                                Pattern pattern = (item.isFolder() || item.isContainer()) ? COPY_FOLDER_PATTERN : COPY_FILE_PATTERN;
                                Matcher matcher = pattern.matcher(childName);
                                if (matcher.matches()) {
                                    int helper = ContentFormatUtils.getIntValue(matcher.group(2));
                                    lastNumber = (helper > lastNumber) ? helper : lastNumber;
                                }
                            }
                        }
                    }
                }
                String nextName = originalName + "-" + ++lastNumber + ext;
                return nextName;
            } else {
            // if parent doesn't exist, it is new item so the current name is available one
            }
        }
    } else {
        // cannot generate a name
        return "";
    }
    // if not found the current name is available
    return levels[length - 1];
}
Also used : Pattern(java.util.regex.Pattern) ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) Matcher(java.util.regex.Matcher) 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