Search in sources :

Example 1 with ItemState

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

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

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

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

Example 5 with ItemState

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

the class ObjectStateServiceImpl method transitionBulk.

@RetryingOperation
@Override
@ValidateParams
public void transitionBulk(@ValidateStringParam(name = "site") String site, List<String> paths, TransitionEvent event, State defaultTargetState) {
    if (paths != null && !paths.isEmpty()) {
        Map<String, Object> params = new HashMap<>();
        params.put("site", site);
        params.put("paths", paths);
        List<ItemState> itemStates = itemStateMapper.getObjectStateForSiteAndPaths(params);
        Map<State, List<String>> bulkSubsets = new HashMap<>();
        for (ItemState state : itemStates) {
            if (!bulkSubsets.containsKey(state.getState())) {
                bulkSubsets.put(State.valueOf(state.getState()), new ArrayList<String>());
            }
            bulkSubsets.get(State.valueOf(state.getState())).add(state.getObjectId());
        }
        State nextState = null;
        for (Map.Entry<State, List<String>> entry : bulkSubsets.entrySet()) {
            if (entry.getKey() == null) {
                params = new HashMap<>();
                params.put("site", site);
                params.put("paths", paths);
                params.put("state", defaultTargetState.name());
                itemStateMapper.setObjectStateForSiteAndPaths(params);
            } else {
                nextState = transitionTable[entry.getKey().ordinal()][event.ordinal()];
                if (nextState != entry.getKey() && nextState != State.NOOP) {
                    params = new HashMap<>();
                    params.put("site", site);
                    params.put("paths", paths);
                    params.put("state", nextState.name());
                    itemStateMapper.setObjectStateForSiteAndPaths(params);
                } else if (nextState == State.NOOP) {
                    logger.warn("Transition not defined for event " + event.name() + " and current state " + entry.getKey().name() + " [setting object state for multiple 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) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation) 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