Search in sources :

Example 11 with SiteNotFoundException

use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.

the class FormDmContentProcessor method createNewFile.

/**
 * create new file to the given path. If the path is a file name, it will
 * create a new folder with the same name as the file name (without the
 * prefix) and move the existing file to the folder created. Then it creates
 * new file to the folder
 *
 * @param site
 *            Site name
 * @param fileName
 *            new file name
 * @param contentType
 * 			content type
 * @param input
 *            file content
 * @param user
 *            current user
 * @throws ContentNotFoundException
 */
protected ContentItemTO createNewFile(String site, ContentItemTO parentItem, String fileName, String contentType, InputStream input, String user, boolean unlock, ResultTO result) throws ContentNotFoundException, SiteNotFoundException {
    ContentItemTO fileItem = null;
    if (parentItem != null) {
        // convert file to folder if target path is a file
        String folderPath = fileToFolder(site, parentItem.getUri());
        try {
            contentService.writeContent(site, parentItem.getUri() + FILE_SEPARATOR + fileName, input);
            if (!objectMetadataManager.metadataExist(site, parentItem.getUri() + FILE_SEPARATOR + fileName)) {
                objectMetadataManager.insertNewObjectMetadata(site, parentItem.getUri() + FILE_SEPARATOR + fileName);
            }
            Map<String, Object> properties = new HashMap<>();
            properties.put(ItemMetadata.PROP_NAME, fileName);
            properties.put(ItemMetadata.PROP_MODIFIED, ZonedDateTime.now(ZoneOffset.UTC));
            properties.put(ItemMetadata.PROP_CREATOR, user);
            properties.put(ItemMetadata.PROP_MODIFIER, user);
            properties.put(ItemMetadata.PROP_OWNER, user);
            if (unlock) {
                properties.put(ItemMetadata.PROP_LOCK_OWNER, StringUtils.EMPTY);
            } else {
                properties.put(ItemMetadata.PROP_LOCK_OWNER, user);
            }
            objectMetadataManager.setObjectMetadata(site, parentItem.getUri() + FILE_SEPARATOR + fileName, properties);
            result.setCommitId(objectMetadataManager.getProperties(site, parentItem.getUri() + FILE_SEPARATOR + fileName).getCommitId());
        } catch (Exception e) {
            logger.error("Error writing new file: " + fileName, e);
        } finally {
            IOUtils.closeQuietly(input);
        }
        // unlock the content upon save
        if (unlock) {
            contentRepository.unLockItem(site, parentItem.getUri() + FILE_SEPARATOR + fileName);
        } else {
        }
        fileItem = contentService.getContentItem(site, parentItem.getUri() + FILE_SEPARATOR + fileName, 0);
        return fileItem;
    } else {
        throw new ContentNotFoundException(parentItem.getUri() + " does not exist in site: " + site);
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) HashMap(java.util.HashMap) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException)

Example 12 with SiteNotFoundException

use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.

the class FormDmContentProcessor method createMissingFoldersInPath.

@Override
public ContentItemTO createMissingFoldersInPath(String site, String path, boolean isPreview) throws SiteNotFoundException {
    // create parent folders if missing
    String[] levels = path.split(FILE_SEPARATOR);
    String parentPath = "";
    ContentItemTO lastItem = null;
    for (String level : levels) {
        if (!StringUtils.isEmpty(level) && !level.endsWith(DmConstants.XML_PATTERN)) {
            String currentPath = parentPath + FILE_SEPARATOR + level;
            if (!contentService.contentExists(site, currentPath)) {
                contentService.createFolder(site, parentPath, level);
            }
            parentPath = currentPath;
        }
    }
    lastItem = contentService.getContentItem(site, parentPath, 0);
    return lastItem;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO)

Example 13 with SiteNotFoundException

use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.

the class PostActivityProcessor method process.

public void process(PipelineContent content, ResultTO result) throws SiteNotFoundException {
    if (result.getCommitId() != null) {
        String site = content.getProperty(DmConstants.KEY_SITE);
        boolean skipAuditLogInsert = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_SKIP_AUDIT_LOG_INSERT));
        if (!skipAuditLogInsert) {
            String type = content.getProperty(DmConstants.KEY_ACTIVITY_TYPE);
            String user = content.getProperty(DmConstants.KEY_USER);
            String activityType = OPERATION_CREATE.equals(type) ? OPERATION_CREATE : OPERATION_UPDATE;
            String folderPath = content.getProperty(DmConstants.KEY_FOLDER_PATH);
            String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
            boolean isSystemAsset = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_SYSTEM_ASSET));
            if (isSystemAsset) {
                ContentAssetInfoTO assetInfoTO = (ContentAssetInfoTO) result.getItem();
                fileName = assetInfoTO.getFileName();
            }
            String uri = (folderPath.endsWith(FILE_SEPARATOR)) ? folderPath + fileName : folderPath + FILE_SEPARATOR + fileName;
            SiteFeed siteFeed = siteService.getSite(site);
            AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
            auditLog.setOperation(activityType);
            auditLog.setActorId(user);
            auditLog.setSiteId(siteFeed.getId());
            auditLog.setPrimaryTargetId(site + ":" + uri);
            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
            auditLog.setPrimaryTargetValue(uri);
            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(site, uri));
            auditServiceInternal.insertAuditLog(auditLog);
        }
        contentRepository.markGitLogAudited(site, result.getCommitId());
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Example 14 with SiteNotFoundException

use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.

the class SiteServiceImpl method setSiteState.

@Override
public void setSiteState(String siteId, String state) {
    siteFeedMapper.setSiteState(siteId, state);
    try {
        ClusterMember clusterMember = clusterDao.getMemberByLocalAddress(studioClusterUtils.getClusterNodeLocalAddress());
        if (Objects.nonNull(clusterMember)) {
            SiteFeed siteFeed = getSite(siteId);
            clusterDao.setSiteState(clusterMember.getId(), siteFeed.getId(), state);
        }
    } catch (SiteNotFoundException e) {
        logger.error("Site not found " + siteId);
    }
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException)

Example 15 with SiteNotFoundException

use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.

the class DependencyServiceImpl method upsertDependencies.

@Override
public Set<String> upsertDependencies(String site, List<String> paths) throws SiteNotFoundException, ContentNotFoundException, ServiceLayerException {
    Set<String> toRet = new HashSet<String>();
    List<DependencyEntity> dependencyEntities = new ArrayList<>();
    StringBuilder sbPaths = new StringBuilder();
    logger.debug("Resolving dependencies for list of paths.");
    for (String path : paths) {
        sbPaths.append("\n").append(path);
        logger.debug("Resolving dependencies for content site: " + site + " path: " + path);
        Map<String, Set<String>> dependencies = dependencyResolver.resolve(site, path);
        if (dependencies != null) {
            logger.debug("Found " + dependencies.size() + " dependencies. " + "Create entities to insert into database.");
            for (String type : dependencies.keySet()) {
                dependencyEntities.addAll(createDependencyEntities(site, path, dependencies.get(type), type, toRet));
            }
        }
    }
    logger.debug("Preparing transaction for database updates.");
    DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
    defaultTransactionDefinition.setName("upsertDependencies");
    String lock = site + ":upsertDependencies";
    generalLockService.lock(lock);
    logger.debug("Starting transaction.");
    TransactionStatus txStatus = transactionManager.getTransaction(defaultTransactionDefinition);
    try {
        logger.debug("Delete all source dependencies for list of paths site: " + site);
        for (String path : paths) {
            deleteAllSourceDependencies(site, path);
        }
        logger.debug("Insert all extracted dependencies entries lof list of paths for site: " + site);
        insertDependenciesIntoDatabase(dependencyEntities);
        logger.debug("Committing transaction.");
        transactionManager.commit(txStatus);
    } catch (Exception e) {
        logger.debug("Rolling back transaction.", e);
        transactionManager.rollback(txStatus);
        throw new ServiceLayerException("Failed to upsert dependencies for site: " + site + " paths: " + sbPaths.toString(), e);
    } finally {
        generalLockService.unlock(lock);
    }
    return toRet;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) DependencyEntity(org.craftercms.studio.api.v1.dal.DependencyEntity) HashSet(java.util.HashSet)

Aggregations

SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)41 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)34 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)23 ResponseBody (org.craftercms.studio.model.rest.ResponseBody)20 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)12 PostMapping (org.springframework.web.bind.annotation.PostMapping)11 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)10 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)9 GetMapping (org.springframework.web.bind.annotation.GetMapping)9 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)8 Result (org.craftercms.studio.model.rest.Result)8 CryptoException (org.craftercms.commons.crypto.CryptoException)6 ResultOne (org.craftercms.studio.model.rest.ResultOne)6 IOException (java.io.IOException)5 Set (java.util.Set)5 ItemMetadata (org.craftercms.studio.api.v1.dal.ItemMetadata)5 AuditLogParameter (org.craftercms.studio.api.v2.dal.AuditLogParameter)5 HashSet (java.util.HashSet)4