Search in sources :

Example 1 with State

use of org.craftercms.studio.api.v1.service.objectstate.State in project studio by craftercms.

the class ContentServiceImpl method populateWorkflowProperties.

protected void populateWorkflowProperties(String site, ContentItemTO item) {
    ItemState state = objectStateService.getObjectState(site, item.getUri(), false);
    if (state != null) {
        if (item.isFolder()) {
            boolean liveFolder = objectStateService.isFolderLive(site, item.getUri());
            item.setNew(!liveFolder);
            item.setLive(liveFolder);
        } else {
            item.setNew(State.isNew(State.valueOf(state.getState())));
            item.setLive(State.isLive(State.valueOf(state.getState())));
        }
        item.isNew = item.isNew();
        item.isLive = item.isLive();
        item.setInProgress(!item.isLive());
        item.isInProgress = item.isInProgress();
        item.setScheduled(State.isScheduled(State.valueOf(state.getState())));
        item.isScheduled = item.isScheduled();
        item.setSubmitted(State.isSubmitted(State.valueOf(state.getState())));
        item.isSubmitted = item.isSubmitted();
        item.setInFlight(state.getSystemProcessing() == 1);
        item.isInFlight = item.isInFlight();
    } else {
        if (item.isFolder()) {
            boolean liveFolder = objectStateService.isFolderLive(site, item.getUri());
            item.setNew(!liveFolder);
            item.setLive(liveFolder);
            item.isNew = item.isNew();
            item.isLive = item.isLive();
            item.setInProgress(!item.isLive());
            item.isInProgress = item.isInProgress();
        }
    }
}
Also used : ItemState(org.craftercms.studio.api.v1.dal.ItemState)

Example 2 with State

use of org.craftercms.studio.api.v1.service.objectstate.State 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 3 with State

use of org.craftercms.studio.api.v1.service.objectstate.State in project studio by craftercms.

the class WorkflowServiceImpl method getInProgressItems.

protected List<ContentItemTO> getInProgressItems(final String site, final DmContentItemComparator comparator, final boolean inProgressOnly) throws ServiceLayerException {
    final List<ContentItemTO> categoryItems = new ArrayList<>();
    List<ContentItemTO> categoryItems1 = getCategoryItems(site);
    categoryItems.addAll(categoryItems1);
    long st = System.currentTimeMillis();
    List<ItemState> changeSet = objectStateService.getChangeSet(site);
    logger.debug("Time taken listChangedAll()  " + (System.currentTimeMillis() - st));
    // the category item to add all other items that do not belong to
    // regular categories specified in the configuration
    st = System.currentTimeMillis();
    if (changeSet != null) {
        List<String> displayPatterns = servicesConfig.getDisplayInWidgetPathPatterns(site);
        // List<String> inProgressItems = new FastList<String>();
        for (ItemState state : changeSet) {
            if (contentService.contentExists(state.getSite(), state.getPath())) {
                if (ContentUtils.matchesPatterns(state.getPath(), displayPatterns)) {
                    ContentItemTO item = contentService.getContentItem(state.getSite(), state.getPath(), 0);
                    addInProgressItems(site, item, categoryItems, comparator, inProgressOnly);
                }
            }
        }
    }
    logger.debug("Time taken after listChangedAll() : " + (System.currentTimeMillis() - st));
    return categoryItems;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ItemState(org.craftercms.studio.api.v1.dal.ItemState) ArrayList(java.util.ArrayList)

Example 4 with State

use of org.craftercms.studio.api.v1.service.objectstate.State in project studio by craftercms.

the class ImportServiceImpl method writeContent.

/**
 * write content
 *
 * @param site
 * @param importedPaths
 * @param importedFullPaths
 * @param fileRoot
 * @param parentPath
 * @param name
 * @param overWrite
 */
protected void writeContent(String site, Set<String> importedPaths, List<String> importedFullPaths, String fileRoot, String targetRoot, String parentPath, String name, boolean overWrite) {
    boolean isXml = true;
    String processChain = getXmlChainName();
    if (!name.endsWith(".xml")) {
        isXml = false;
        processChain = getAssetChainName();
    }
    InputStream in = null;
    String filePath = parentPath + FILE_SEPARATOR + name;
    String fileSystemPath = fileRoot + FILE_SEPARATOR + name;
    logger.info("[IMPORT] writeContent: fileRoot [" + fileRoot + "] fullPath [" + filePath + "] overwrite[" + overWrite + "] process chain [ " + processChain + "]");
    long startTimeWrite = System.currentTimeMillis();
    logger.debug("[IMPORT] writing file: " + parentPath + FILE_SEPARATOR + name);
    try {
        File file = new File(fileSystemPath);
        if (file.exists()) {
            in = new FileInputStream(file);
            String currentPath = parentPath + FILE_SEPARATOR + name;
            boolean contentExists = contentService.contentExists(site, currentPath);
            // create parameters
            Map<String, String> params = createParams(site, isXml, targetRoot, parentPath, name);
            String id = site + ":" + filePath + ":" + name;
            // existing
            if (!contentExists || overWrite) {
                String fullPath = targetRoot + filePath;
                objectStateService.setSystemProcessing(site, currentPath, true);
                // write the content
                contentService.processContent(id, in, isXml, params, processChain);
                ContentItemTO item = contentService.getContentItem(site, currentPath);
                // update state
                if (item != null) {
                    objectStateService.transition(site, item, TransitionEvent.SAVE);
                    objectStateService.setSystemProcessing(site, currentPath, false);
                } else {
                    ItemState state = objectStateService.getObjectState(site, currentPath);
                    if (state == null) {
                        objectStateService.insertNewEntry(site, currentPath);
                    }
                }
                importedPaths.add(filePath);
                importedFullPaths.add(fullPath);
            } else {
                logger.debug("[IMPORT] " + filePath + " exists and set to not to overrwite. skipping this file.");
            }
        }
    } catch (FileNotFoundException e) {
        logger.warn("[IMPORT] " + filePath + " does not exist.");
    } catch (ServiceLayerException e) {
        logger.error("[IMPORT] failed to import " + filePath, e);
    } finally {
        ContentUtils.release(in);
    }
    logger.debug("[IMPORT] done writing file: " + parentPath + FILE_SEPARATOR + name + ", time: " + (System.currentTimeMillis() - startTimeWrite));
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ItemState(org.craftercms.studio.api.v1.dal.ItemState) FileNotFoundException(java.io.FileNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with State

use of org.craftercms.studio.api.v1.service.objectstate.State in project studio by craftercms.

the class ObjectStateServiceImpl method insertNewEntry.

@Override
@ValidateParams
public void insertNewEntry(@ValidateStringParam(name = "site") String site, ContentItemTO item) {
    String path = FilenameUtils.normalize(item.getUri(), true);
    String lockKey = site + ":" + path;
    generalLockService.lock(lockKey);
    try {
        Map<String, String> params = new HashMap<String, String>();
        params.put("site", site);
        params.put("path", path);
        ItemState state = itemStateMapper.getObjectStateBySiteAndPath(params);
        if (state == null) {
            ItemState newEntry = new ItemState();
            if (StringUtils.isEmpty(item.getNodeRef())) {
                newEntry.setObjectId(UUID.randomUUID().toString());
            } else {
                newEntry.setObjectId(item.getNodeRef());
            }
            newEntry.setSite(site);
            newEntry.setPath(path);
            newEntry.setSystemProcessing(0);
            newEntry.setState(State.NEW_UNPUBLISHED_UNLOCKED.name());
            itemStateMapper.insertEntry(newEntry);
        }
    } finally {
        generalLockService.unlock(lockKey);
    }
}
Also used : HashMap(java.util.HashMap) ItemState(org.craftercms.studio.api.v1.dal.ItemState) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Aggregations

ItemState (org.craftercms.studio.api.v1.dal.ItemState)24 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)19 HashMap (java.util.HashMap)14 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)8 ArrayList (java.util.ArrayList)7 State (org.craftercms.studio.api.v1.service.objectstate.State)6 ItemMetadata (org.craftercms.studio.api.v1.dal.ItemMetadata)4 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)4 RetryingOperation (org.craftercms.studio.api.v2.annotation.RetryingOperation)3 Map (java.util.Map)2 EntitlementException (org.craftercms.commons.entitlements.exception.EntitlementException)2 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)2 PreviewEventContext (org.craftercms.studio.api.v1.ebus.PreviewEventContext)2 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)2 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)2 DeploymentException (org.craftercms.studio.api.v1.service.deployment.DeploymentException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1