Search in sources :

Example 21 with ItemState

use of org.craftercms.studio.api.v1.dal.ItemState 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 22 with ItemState

use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.

the class ObjectStateServiceImpl method transition.

@RetryingOperation
@Override
@ValidateParams
public void transition(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, TransitionEvent event) {
    String itemPath = FilenameUtils.normalize(path, true);
    String lockKey = site + ":" + path;
    generalLockService.lock(lockKey);
    try {
        Map<String, String> params = new HashMap<String, String>();
        params.put("site", site);
        params.put("path", itemPath);
        ItemState currentState = itemStateMapper.getObjectStateBySiteAndPath(params);
        State nextState = null;
        if (currentState == null) {
            logger.debug("Preforming transition event " + event.name() + " on object " + lockKey + " without current state");
            switch(event) {
                case SAVE:
                    nextState = State.NEW_UNPUBLISHED_UNLOCKED;
                    break;
                case SAVE_FOR_PREVIEW:
                    nextState = State.NEW_UNPUBLISHED_LOCKED;
                    break;
                default:
                    nextState = State.NEW_UNPUBLISHED_UNLOCKED;
            }
        } else {
            logger.debug("Preforming transition event " + event + " on object " + lockKey + " with " + currentState.getState() + " state");
            State currentStateValue = State.valueOf(currentState.getState());
            nextState = transitionTable[currentStateValue.ordinal()][event.ordinal()];
        }
        if (currentState == null) {
            ItemState newEntry = new ItemState();
            newEntry.setObjectId(UUID.randomUUID().toString());
            newEntry.setSite(site);
            newEntry.setPath(itemPath);
            newEntry.setSystemProcessing(0);
            newEntry.setState(nextState.name());
            itemStateMapper.insertEntry(newEntry);
        } else if (nextState.toString() != currentState.getState() && nextState != State.NOOP) {
            currentState.setState(nextState.name());
            itemStateMapper.setObjectState(currentState);
        } else if (nextState == State.NOOP) {
            logger.warn("Transition not defined for event " + event.name() + " and current state " + currentState.getState() + " [object id: " + currentState.getObjectId() + "]");
        }
    } catch (Exception e) {
        logger.error("Transition not defined for event", e);
    } finally {
        generalLockService.unlock(lockKey);
    }
    logger.debug("Transition finished for " + event.name() + " on object " + lockKey);
}
Also used : HashMap(java.util.HashMap) State(org.craftercms.studio.api.v1.service.objectstate.State) ItemState(org.craftercms.studio.api.v1.dal.ItemState) ItemState(org.craftercms.studio.api.v1.dal.ItemState) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 23 with ItemState

use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.

the class ObjectStateServiceImpl method setObjectState.

@RetryingOperation
@Override
@ValidateParams
public String setObjectState(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, @ValidateStringParam(name = "state") String state, boolean systemProcessing) {
    path = FilenameUtils.normalize(path, true);
    Map<String, String> params = new HashMap<String, String>();
    params.put("site", site);
    params.put("path", path);
    ItemState objectState = itemStateMapper.getObjectStateBySiteAndPath(params);
    if (objectState == null) {
        insertNewEntry(site, path);
        objectState = itemStateMapper.getObjectStateBySiteAndPath(params);
    }
    objectState.setState(state);
    objectState.setSystemProcessing(systemProcessing ? 1 : 0);
    itemStateMapper.setObjectState(objectState);
    return "Success";
}
Also used : HashMap(java.util.HashMap) ItemState(org.craftercms.studio.api.v1.dal.ItemState) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 24 with ItemState

use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.

the class ObjectStateServiceImpl method isUpdated.

@Override
@ValidateParams
public boolean isUpdated(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path) {
    path = FilenameUtils.normalize(path, true);
    ItemState state = getObjectState(site, path);
    if (state != null) {
        return State.isUpdated(State.valueOf(state.getState()));
    } else {
        return false;
    }
}
Also used : ItemState(org.craftercms.studio.api.v1.dal.ItemState) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 25 with ItemState

use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.

the class ObjectStateServiceImpl method getChangeSetForSubtree.

@Override
@ValidateParams
public List<String> getChangeSetForSubtree(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("site", site);
    params.put("path", path);
    params.put("likepath", path + (path.endsWith(FILE_SEPARATOR) ? "" : FILE_SEPARATOR) + "%");
    params.put("states", State.CHANGE_SET_STATES);
    List<ItemState> result = itemStateMapper.getChangeSetForSubtree(params);
    List<String> toRet = new ArrayList<String>();
    for (ItemState state : result) {
        toRet.add(state.getPath());
    }
    return toRet;
}
Also used : HashMap(java.util.HashMap) ItemState(org.craftercms.studio.api.v1.dal.ItemState) ArrayList(java.util.ArrayList) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Aggregations

ItemState (org.craftercms.studio.api.v1.dal.ItemState)25 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)19 HashMap (java.util.HashMap)12 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)7 ArrayList (java.util.ArrayList)5 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)5 State (org.craftercms.studio.api.v1.service.objectstate.State)5 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)3 RetryingOperation (org.craftercms.studio.api.v2.annotation.RetryingOperation)3 InputStream (java.io.InputStream)2 Map (java.util.Map)2 CryptoException (org.craftercms.commons.crypto.CryptoException)2 EntitlementException (org.craftercms.commons.entitlements.exception.EntitlementException)2 PreviewEventContext (org.craftercms.studio.api.v1.ebus.PreviewEventContext)2 DocumentException (org.dom4j.DocumentException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ZonedDateTime (java.time.ZonedDateTime)1