Search in sources :

Example 1 with EVENT_PREVIEW_SYNC

use of org.craftercms.studio.api.v1.ebus.EBusConstants.EVENT_PREVIEW_SYNC in project studio by craftercms.

the class ContentServiceImpl method moveContent.

@Override
@ValidateParams
public String moveContent(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "fromPath") String fromPath, @ValidateSecurePathParam(name = "toPath") String toPath) {
    String retNewFileName = null;
    boolean opSuccess = false;
    String movePath = null;
    try {
        String sourcePath = (fromPath.indexOf("" + FILE_SEPARATOR + DmConstants.INDEX_FILE) != -1) ? fromPath.substring(0, fromPath.lastIndexOf(FILE_SEPARATOR)) : fromPath;
        String sourcePathOnly = fromPath.substring(0, fromPath.lastIndexOf(FILE_SEPARATOR));
        Map<String, String> movePathMap = constructNewPathforCutCopy(site, fromPath, toPath, true);
        movePath = movePathMap.get("FILE_PATH");
        String moveFileName = movePathMap.get("FILE_NAME");
        String movePathOnly = movePath.substring(0, movePath.lastIndexOf(FILE_SEPARATOR));
        boolean moveAltFileName = "true".equals(movePathMap.get("ALT_NAME"));
        boolean targetIsIndex = DmConstants.INDEX_FILE.equals(moveFileName);
        boolean sourceIsIndex = DmConstants.INDEX_FILE.equals(fromPath);
        String targetPath = movePathOnly;
        if (movePathOnly.equals(sourcePathOnly) || (moveAltFileName == true && !targetIsIndex) || (!sourceIsIndex && !targetIsIndex)) {
            // we never send index.xml to the repo, we move folders (and the folder has the rename)
            // SO otherwise, this is a rename and we need to forward the full path
            targetPath = movePath;
        }
        logger.debug("Move file for site {0} from {1} to {2}, sourcePath {3} to target path {4}", site, fromPath, toPath, sourcePath, targetPath);
        // NOTE: IN WRITE SCENARIOS the repository OP IS PART of this PIPELINE, for some reason,
        // historically with MOVE it is not
        Map<String, String> commitIds = _contentRepository.moveContent(site, sourcePath, targetPath);
        if (commitIds != null) {
            // Update the database with the commitId for the target item
            updateDatabaseOnMove(site, fromPath, movePath);
            updateChildrenOnMove(site, fromPath, movePath);
            for (Map.Entry<String, String> entry : commitIds.entrySet()) {
                objectMetadataManager.updateCommitId(site, FILE_SEPARATOR + entry.getKey(), entry.getValue());
                contentRepository.insertGitLog(site, entry.getValue(), 1, 1);
            }
            siteService.updateLastCommitId(site, _contentRepository.getRepoLastCommitId(site));
        } else {
            logger.error("Repository move failed site {0} from {1} to {2}", site, sourcePath, targetPath);
            movePath = fromPath;
        }
        PreviewEventContext context = new PreviewEventContext();
        context.setSite(site);
        eventService.publish(EVENT_PREVIEW_SYNC, context);
    } catch (ServiceLayerException eMoveErr) {
        logger.error("Content not found while moving content for site {0} from {1} to {2}, new name is {3}", eMoveErr, site, fromPath, toPath, movePath);
    }
    return movePath;
}
Also used : ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) 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 2 with EVENT_PREVIEW_SYNC

use of org.craftercms.studio.api.v1.ebus.EBusConstants.EVENT_PREVIEW_SYNC in project studio by craftercms.

the class ClipboardServiceImpl method pasteItems.

/**
 * Recursive paste operation on item and it's children
 * @param site site ID
 * @param destinationPath destination path for itme
 * @param clipOps ops to be pasted
 * @param pastedItems collection of (new) pasted paths
 */
protected void pasteItems(String site, String destinationPath, Set<ClipboardItem> clipOps, Set<String> pastedItems) throws ServiceLayerException {
    for (ClipboardItem op : clipOps) {
        try {
            String newPath = null;
            boolean cut = op.isCut;
            if (cut) {
                // RDTMP_COPYPASTE
                // CopyContent inteface is able to send status and new path yet
                workflowService.cleanWorkflow(op.path, site, Collections.<DmDependencyTO>emptySet());
                newPath = contentService.moveContent(site, op.path, destinationPath);
            } else {
                // RDTMP_COPYPASTE
                // CopyContent inteface is able to send status and new path yet
                newPath = contentService.copyContent(site, op.path, destinationPath);
                // recurse on copied children
                pasteItems(site, newPath, op.children, pastedItems);
            }
            pastedItems.add(newPath);
        } catch (Exception err) {
            logger.error("Paste operation failed for item '{0}' to dest path `{1}', isCut: '{2}'", err);
        }
    }
    // trigger preview deploy
    PreviewEventContext context = new PreviewEventContext();
    context.setSite(site);
    eventService.publish(EVENT_PREVIEW_SYNC, context);
}
Also used : ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext)

Example 3 with EVENT_PREVIEW_SYNC

use of org.craftercms.studio.api.v1.ebus.EBusConstants.EVENT_PREVIEW_SYNC in project studio by craftercms.

the class SiteServiceImpl method writeConfiguration.

@Override
@ValidateParams
public boolean writeConfiguration(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, InputStream content) throws ServiceLayerException {
    // Write site configuration
    String operation = OPERATION_UPDATE;
    if (!contentRepository.contentExists(site, path)) {
        operation = OPERATION_CREATE;
    }
    String commitId = contentRepository.writeContent(site, path, content);
    contentRepository.reloadRepository(site);
    PreviewEventContext context = new PreviewEventContext();
    context.setSite(site);
    eventService.publish(EVENT_PREVIEW_SYNC, context);
    String user = securityService.getCurrentUser();
    Map<String, String> extraInfo = new HashMap<String, String>();
    if (StringUtils.startsWith(path, contentTypeService.getConfigPath())) {
        extraInfo.put(DmConstants.KEY_CONTENT_TYPE, StudioConstants.CONTENT_TYPE_CONTENT_TYPE);
    } else {
        extraInfo.put(DmConstants.KEY_CONTENT_TYPE, StudioConstants.CONTENT_TYPE_CONFIGURATION);
    }
    SiteFeed siteFeed = getSite(site);
    AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
    auditLog.setOperation(operation);
    auditLog.setSiteId(siteFeed.getId());
    auditLog.setActorId(user);
    auditLog.setPrimaryTargetId(site + ":" + path);
    auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
    auditLog.setPrimaryTargetValue(path);
    auditServiceInternal.insertAuditLog(auditLog);
    objectStateService.transition(site, path, TransitionEvent.SAVE);
    if (!objectMetadataManager.metadataExist(site, path)) {
        objectMetadataManager.insertNewObjectMetadata(site, path);
    }
    Map<String, Object> properties = new HashMap<>();
    properties.put(ItemMetadata.PROP_NAME, FilenameUtils.getName(path));
    properties.put(ItemMetadata.PROP_MODIFIED, ZonedDateTime.now(ZoneOffset.UTC));
    properties.put(ItemMetadata.PROP_MODIFIER, user);
    objectMetadataManager.setObjectMetadata(site, path, properties);
    if (commitId != null) {
        objectMetadataManager.updateCommitId(site, path, commitId);
        contentRepositoryV2.insertGitLog(site, commitId, 1);
    }
    boolean toRet = StringUtils.isEmpty(commitId);
    return toRet;
}
Also used : HashMap(java.util.HashMap) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 4 with EVENT_PREVIEW_SYNC

use of org.craftercms.studio.api.v1.ebus.EBusConstants.EVENT_PREVIEW_SYNC in project studio by craftercms.

the class ConfigurationServiceImpl method writeConfiguration.

@Override
@HasPermission(type = DefaultPermission.class, action = "write_configuration")
public void writeConfiguration(@ProtectedResourceId(SITE_ID_RESOURCE_ID) String siteId, String module, String path, String environment, InputStream content) throws ServiceLayerException {
    writeEnvironmentConfiguration(siteId, module, path, environment, content);
    PreviewEventContext context = new PreviewEventContext();
    context.setSite(siteId);
    eventService.publish(EVENT_PREVIEW_SYNC, context);
}
Also used : PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 5 with EVENT_PREVIEW_SYNC

use of org.craftercms.studio.api.v1.ebus.EBusConstants.EVENT_PREVIEW_SYNC in project studio by craftercms.

the class StudioClusterSandboxRepoSyncTask method updateContent.

protected void updateContent(long localNodeId, long sId, String siteId, String sandboxBranchName, List<ClusterMember> clusterNodes) throws IOException, CryptoException, ServiceLayerException {
    logger.debug("Update sandbox for site " + siteId);
    Path siteSandboxPath = buildRepoPath(siteId).resolve(GIT_ROOT);
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(siteSandboxPath.toFile()).readEnvironment().findGitDir().build();
    Map<String, String> remoteLastSyncCommits = remotesMap.get(siteId);
    if (remoteLastSyncCommits == null || remoteLastSyncCommits.isEmpty()) {
        remoteLastSyncCommits = new HashMap<String, String>();
        remotesMap.put(siteId, remoteLastSyncCommits);
    }
    try (Git git = new Git(repo)) {
        logger.debug("Update content from each active cluster memeber");
        for (ClusterMember remoteNode : clusterNodes) {
            ClusterSiteRecord csr = clusterDao.getClusterSiteRecord(remoteNode.getId(), sId);
            if (Objects.nonNull(csr) && StringUtils.equals(csr.getState(), STATE_CREATED)) {
                updateBranch(siteId, git, remoteNode, sandboxBranchName);
            }
        }
        String updatedCommitId = contentRepository.getRepoLastCommitId(siteId);
        clusterDao.updateNodeLastCommitId(localNodeId, sId, updatedCommitId);
        PreviewEventContext context = new PreviewEventContext();
        context.setSite(siteId);
        eventService.publish(EVENT_PREVIEW_SYNC, context);
    } catch (GitAPIException e) {
        logger.error("Error while syncing cluster node content for site " + siteId);
    }
}
Also used : Path(java.nio.file.Path) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) Git(org.eclipse.jgit.api.Git) ClusterSiteRecord(org.craftercms.studio.api.v2.dal.ClusterSiteRecord) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) PreviewEventContext(org.craftercms.studio.api.v1.ebus.PreviewEventContext)

Aggregations

PreviewEventContext (org.craftercms.studio.api.v1.ebus.PreviewEventContext)11 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)8 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)6 HashMap (java.util.HashMap)5 Map (java.util.Map)3 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)3 EntitlementException (org.craftercms.commons.entitlements.exception.EntitlementException)2 ItemState (org.craftercms.studio.api.v1.dal.ItemState)2 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)2 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)2 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1 HasPermission (org.craftercms.commons.security.permissions.annotations.HasPermission)1 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)1 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)1 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)1 AuthenticationException (org.craftercms.studio.api.v1.exception.security.AuthenticationException)1 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)1