Search in sources :

Example 16 with ItemState

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

the class ContentServiceImpl method writeContentAsset.

/**
 * write content asset
 *
 * @param site
 * @param path
 * @param assetName
 * @param in
 * @param isImage
 * 			is this asset an image?
 * @param allowedWidth
 * 			specifies the allowed image width in pixel if the asset is an image
 * @param allowedHeight
 * 			specifies the allowed image height in pixel if the asset is an image
 * @param unlock
 * 			unlock the content upon edit?
 * @return content asset info
 * @throws ServiceLayerException
 */
@Override
@ValidateParams
public Map<String, Object> writeContentAsset(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, @ValidateStringParam(name = "assetName") String assetName, InputStream in, String isImage, String allowedWidth, String allowedHeight, String allowLessSize, String draft, String unlock, String systemAsset) throws ServiceLayerException {
    try {
        entitlementValidator.validateEntitlement(EntitlementType.ITEM, 1);
    } catch (EntitlementException e) {
        throw new ServiceLayerException("Unable to complete request due to entitlement limits. Please contact your " + "system administrator.");
    }
    boolean isSystemAsset = Boolean.valueOf(systemAsset);
    Map<String, String> params = new HashMap<String, String>();
    params.put(DmConstants.KEY_SITE, site);
    params.put(DmConstants.KEY_PATH, path);
    params.put(DmConstants.KEY_FILE_NAME, assetName);
    params.put(DmConstants.KEY_IS_IMAGE, isImage);
    params.put(DmConstants.KEY_ALLOW_LESS_SIZE, allowLessSize);
    params.put(DmConstants.KEY_ALLOWED_WIDTH, allowedWidth);
    params.put(DmConstants.KEY_ALLOWED_HEIGHT, allowedHeight);
    params.put(DmConstants.KEY_CONTENT_TYPE, "");
    params.put(DmConstants.KEY_CREATE_FOLDERS, "true");
    params.put(DmConstants.KEY_UNLOCK, unlock);
    params.put(DmConstants.KEY_SYSTEM_ASSET, String.valueOf(isSystemAsset));
    boolean exists = contentExists(site, path + FILE_SEPARATOR + assetName);
    params.put(DmConstants.KEY_ACTIVITY_TYPE, (exists ? OPERATION_UPDATE : OPERATION_CREATE));
    String id = site + ":" + path + ":" + assetName + ":" + "";
    // processContent will close the input stream
    ContentItemTO item = null;
    try {
        path = path + FILE_SEPARATOR + assetName;
        item = getContentItem(site, path);
        if (item != null) {
            ItemState itemState = objectStateService.getObjectState(site, path);
            if (itemState != null) {
                if (itemState.getSystemProcessing() != 0) {
                    logger.error(String.format("Error Content %s is being processed " + "(Object State is SYSTEM_PROCESSING);", path));
                    throw new RuntimeException(String.format("Content \"%s\" is being processed", path));
                }
                objectStateService.setSystemProcessing(site, path, true);
            }
        }
        if (objectStateService.deletedPathExists(site, path) || objectMetadataManager.movedPathExists(site, path)) {
            throw new ServiceLayerException("Content " + path + " for site " + site + ", cannot be created because" + " this name/URL was in use by another content item that has been moved or deleted by " + "not yet published.");
        }
        ResultTO result = processContent(id, in, false, params, DmConstants.CONTENT_CHAIN_ASSET);
        ContentAssetInfoTO assetInfoTO = (ContentAssetInfoTO) result.getItem();
        if (isSystemAsset) {
            path = path.replace(assetName, assetInfoTO.getFileName());
        }
        item = getContentItem(site, path);
        item.setSize(assetInfoTO.getSize());
        item.setSizeUnit(assetInfoTO.getSizeUnit());
        if (item != null) {
            if (result.getCommitId() != null) {
                objectStateService.transition(site, item, SAVE);
            } else {
                objectStateService.transition(site, item, TransitionEvent.CANCEL_EDIT);
            }
        }
        PreviewEventContext context = new PreviewEventContext();
        context.setSite(site);
        eventService.publish(EVENT_PREVIEW_SYNC, context);
        Map<String, Object> toRet = new HashMap<String, Object>();
        toRet.put("success", true);
        toRet.put("message", item);
        return toRet;
    } catch (Exception e) {
        logger.error("Error processing content", e);
        Map<String, Object> toRet = new HashMap<String, Object>();
        toRet.put("success", true);
        toRet.put("message", e.getMessage());
        toRet.put("error", e);
        return toRet;
    } finally {
        if (item != null) {
            objectStateService.setSystemProcessing(site, path, false);
        }
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) HashMap(java.util.HashMap) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ResultTO(org.craftercms.studio.api.v1.to.ResultTO) 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) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ItemState(org.craftercms.studio.api.v1.dal.ItemState) Map(java.util.Map) HashMap(java.util.HashMap) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 17 with ItemState

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

the class ObjectStateServiceImpl method isNew.

@Override
@ValidateParams
public boolean isNew(@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.isNew(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 18 with ItemState

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

the class ObjectStateServiceImpl method getSubmittedItems.

@Override
@ValidateParams
public List<ItemState> getSubmittedItems(@ValidateStringParam(name = "site") String site) {
    Map<String, Object> params = new HashMap<String, Object>();
    List<String> statesValues = new ArrayList<String>();
    for (State state : State.SUBMITTED_STATES) {
        statesValues.add(state.name());
    }
    params.put("states", statesValues);
    params.put("site", site);
    List<ItemState> objects = itemStateMapper.getObjectStateByStates(params);
    return objects;
}
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) ArrayList(java.util.ArrayList) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 19 with ItemState

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

the class ObjectStateServiceImpl method isScheduled.

@Override
@ValidateParams
public boolean isScheduled(@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.isScheduled(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 20 with ItemState

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

the class ContentServiceImpl method writeContent.

@Override
@ValidateParams
public void writeContent(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, @ValidateStringParam(name = "fileName") String fileName, @ValidateStringParam(name = "contentType") String contentType, InputStream input, @ValidateStringParam(name = "createFolders") String createFolders, @ValidateStringParam(name = "edit") String edit, @ValidateStringParam(name = "unlock") String unlock, boolean skipAuditLogInsert) throws ServiceLayerException {
    try {
        entitlementValidator.validateEntitlement(EntitlementType.ITEM, 1);
    } catch (EntitlementException e) {
        throw new ServiceLayerException("Unable to complete request due to entitlement limits. Please contact your " + "system administrator.");
    }
    Map<String, String> params = new HashMap<String, String>();
    params.put(DmConstants.KEY_SITE, site);
    params.put(DmConstants.KEY_PATH, path);
    params.put(DmConstants.KEY_FILE_NAME, fileName);
    params.put(DmConstants.KEY_CONTENT_TYPE, contentType);
    params.put(DmConstants.KEY_CREATE_FOLDERS, createFolders);
    params.put(DmConstants.KEY_EDIT, edit);
    params.put(DmConstants.KEY_UNLOCK, unlock);
    params.put(DmConstants.KEY_SKIP_AUDIT_LOG_INSERT, String.valueOf(skipAuditLogInsert));
    String id = site + ":" + path + ":" + fileName + ":" + contentType;
    String relativePath = path;
    boolean contentExists = contentExists(site, path);
    String lockKey = id;
    if (contentExists) {
        lockKey = site + ":" + path;
    }
    try {
        // Check if the user is saving and closing (releasing the lock) or just saving and will continue to edit
        // If "unlock" is empty, it means it's a save and close operation
        // if "unlock" is set to "false", it also means it's a save and continue operation
        boolean isSaveAndClose = (StringUtils.isNotEmpty(unlock) && !unlock.equalsIgnoreCase("false"));
        if (contentExists) {
            ItemState itemState = objectStateService.getObjectState(site, path);
            if (itemState == null) {
                // This file is either new or someone created it outside of our system, we must create a state
                // for it
                ContentItemTO item = getContentItem(site, path, 0);
                objectStateService.insertNewEntry(site, item);
                itemState = objectStateService.getObjectState(site, path);
            }
            if (itemState != null) {
                if (itemState.getSystemProcessing() != 0) {
                    // TODO: SJ: Review and refactor/redo
                    logger.error("Error Content {0} is being processed (Object State is system " + "processing);", path);
                    throw new ServiceLayerException("Content " + path + " is in system processing, we can't write " + "it");
                }
                objectStateService.setSystemProcessing(site, path, true);
            } else {
                logger.error("the object state is still null even after attempting to create it for site {0} " + "path {1} fileName {2} contentType {3}" + ".", site, path, fileName, contentType);
            }
        } else {
            // Content does not exist; check for moved content and deleted content
            if (objectStateService.deletedPathExists(site, path) || objectMetadataManager.movedPathExists(site, path)) {
                throw new ServiceLayerException("Content " + path + " for site " + site + ", cannot be created " + "because this name/URL was in use by another content item that has been moved or" + " deleted by not yet published.");
            }
        }
        // TODO: SJ: Item processing pipeline needs to be configurable without hardcoded paths
        // TODO: SJ: We need to consider various mechanics for pipeline choice other than path
        // TODO: SJ: Furthermore, we already have similar machinery in Crafter Core that might be a fit for some
        // TODO: SJ: of this work
        // default chain is asset type
        String chainID = DmConstants.CONTENT_CHAIN_ASSET;
        if (path.startsWith("/site")) {
            // anything inside site is a form based XML
            // example /site/website
            // /site/components
            // /site/books
            chainID = DmConstants.CONTENT_CHAIN_FORM;
        }
        // TODO: SJ: Content is being written here via the pipeline, this is not the best design and will be
        // TODO: SJ: refactored in 2.7.x
        processContent(id, input, true, params, chainID);
        // Item has been processed and persisted, set system processing state to off
        objectStateService.setSystemProcessing(site, path, false);
        // TODO: SJ: The path sent from the UI is inconsistent, hence the acrobatics below. Fix in 2.7.x
        String savedFileName = params.get(DmConstants.KEY_FILE_NAME);
        String savedPath = params.get(DmConstants.KEY_PATH);
        relativePath = savedPath;
        if (!savedPath.endsWith(savedFileName)) {
            relativePath = savedPath + FILE_SEPARATOR + savedFileName;
        }
        // TODO: SJ: Why is the item being loaded again? Why is the state being set to system not processing
        // TODO: SJ: again? Why would we insert the item into objectStateService again?
        // TODO: SJ: Refactor for 2.7.x
        ContentItemTO itemTo = getContentItem(site, relativePath, 0);
        if (itemTo != null) {
            if (isSaveAndClose) {
                objectStateService.transition(site, itemTo, SAVE);
            } else {
                objectStateService.transition(site, itemTo, SAVE_FOR_PREVIEW);
            }
            objectStateService.setSystemProcessing(site, itemTo.getUri(), false);
        } else {
            // TODO: SJ: the line below doesn't make any sense, itemTo == null => insert? Investigate and fix in
            // TODO: SJ: 2.7.x
            objectStateService.insertNewEntry(site, itemTo);
        }
        // Sync preview
        PreviewEventContext context = new PreviewEventContext();
        context.setSite(site);
        eventService.publish(EVENT_PREVIEW_SYNC, context);
    } catch (RuntimeException e) {
        logger.error("error writing content", e);
        // TODO: SJ: Why setting two things? Are we guessing? Fix in 2.7.x
        objectStateService.setSystemProcessing(site, relativePath, false);
        objectStateService.setSystemProcessing(site, path, false);
        throw e;
    }
}
Also used : EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) HashMap(java.util.HashMap) ItemState(org.craftercms.studio.api.v1.dal.ItemState) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext) 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