Search in sources :

Example 1 with PublishRequest

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

the class PublishServiceInternalImpl method getPublishingPackageDetails.

@Override
public PublishingPackageDetails getPublishingPackageDetails(String siteId, String packageId) {
    List<PublishRequest> publishingRequests = publishRequestDao.getPublishingPackageDetails(siteId, packageId);
    PublishingPackageDetails publishingPackageDetails = new PublishingPackageDetails();
    List<PublishingPackageDetails.PublishingPackageItem> packageItems = new ArrayList<PublishingPackageDetails.PublishingPackageItem>();
    if (CollectionUtils.isNotEmpty(publishingRequests)) {
        PublishRequest pr = publishingRequests.get(0);
        publishingPackageDetails.setSiteId(pr.getSite());
        publishingPackageDetails.setPackageId(pr.getPackageId());
        publishingPackageDetails.setEnvironment(pr.getEnvironment());
        publishingPackageDetails.setState(pr.getState());
        publishingPackageDetails.setScheduledDate(pr.getScheduledDate());
        publishingPackageDetails.setUser(pr.getUser());
        publishingPackageDetails.setComment(pr.getSubmissionComment());
    }
    for (PublishRequest publishRequest : publishingRequests) {
        PublishingPackageDetails.PublishingPackageItem item = new PublishingPackageDetails.PublishingPackageItem();
        item.setPath(publishRequest.getPath());
        item.setContentTypeClass(publishRequest.getContentTypeClass());
        packageItems.add(item);
    }
    publishingPackageDetails.setItems(packageItems);
    return publishingPackageDetails;
}
Also used : ArrayList(java.util.ArrayList) PublishRequest(org.craftercms.studio.api.v2.dal.PublishRequest) PublishingPackageDetails(org.craftercms.studio.api.v2.dal.PublishingPackageDetails)

Example 2 with PublishRequest

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

the class StudioPublisherTask method doPublishing.

private void doPublishing(String siteId, List<PublishRequest> itemsToDeploy, String environment) throws DeploymentException, ServiceLayerException {
    String statusMessage;
    String status;
    String author = itemsToDeploy.get(0).getUser();
    StringBuilder sbComment = new StringBuilder();
    List<DeploymentItemTO> completeDeploymentItemList = new ArrayList<DeploymentItemTO>();
    Set<String> processedPaths = new HashSet<String>();
    SimpleDateFormat sdf = new SimpleDateFormat(StudioConstants.DATE_PATTERN_WORKFLOW_WITH_TZ);
    String currentPackageId = StringUtils.EMPTY;
    try {
        logger.debug("Mark items as processing for site \"{0}\"", siteId);
        Set<String> packageIds = new HashSet<String>();
        int idx = 0;
        for (PublishRequest item : itemsToDeploy) {
            idx++;
            if (!StringUtils.equals(currentPackageId, item.getPackageId())) {
                currentPackageId = item.getPackageId();
            }
            statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_PUBLISHING);
            statusMessage = statusMessage.replace("{package_id}", currentPackageId).replace("{datetime}", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(sdf.toPattern()))).replace("{x}", Integer.toString(idx)).replace("{y}", Integer.toString(itemsToDeploy.size()));
            siteService.updatePublishingStatusMessage(siteId, PUBLISHING, statusMessage);
            processPublishingRequest(siteId, environment, item, completeDeploymentItemList, processedPaths);
            if (packageIds.add(item.getPackageId())) {
                sbComment.append(item.getSubmissionComment()).append("\n");
            }
        }
        deploy(siteId, environment, completeDeploymentItemList, author, sbComment.toString());
        StringBuilder sbPackIds = new StringBuilder("Package(s): ");
        for (String packageId : packageIds) {
            sbPackIds.append(packageId).append(";");
        }
        generateWorkflowActivity(siteId, environment, packageIds, author, OPERATION_PUBLISHED);
        publishingManager.markItemsCompleted(siteId, environment, itemsToDeploy);
        logger.debug("Mark deployment completed for processed items for site \"{0}\"", siteId);
        logger.info("Finished publishing environment " + environment + " for site " + siteId);
        if (publishingManager.isPublishingQueueEmpty(siteId)) {
            status = READY;
            statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_READY);
            statusMessage = statusMessage.replace("{package_id}", currentPackageId).replace("{datetime}", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(sdf.toPattern()))).replace("{package_size}", Integer.toString(itemsToDeploy.size()));
        } else {
            status = QUEUED;
            statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_QUEUED);
        }
        siteService.updatePublishingStatusMessage(siteId, status, statusMessage);
    } catch (DeploymentException err) {
        logger.error("Error while executing deployment to environment store " + "for site \"{0}\", number of items \"{1}\"", err, siteId, itemsToDeploy.size());
        publishingManager.markItemsReady(siteId, environment, itemsToDeploy);
        siteService.enablePublishing(siteId, false);
        statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_ERROR);
        siteService.updatePublishingStatusMessage(siteId, ERROR, statusMessage);
        throw err;
    } catch (Exception err) {
        logger.error("Unexpected error while executing deployment to environment " + "store for site \"{0}\", number of items \"{1}\"", err, siteId, itemsToDeploy.size());
        publishingManager.markItemsReady(siteId, environment, itemsToDeploy);
        siteService.enablePublishing(siteId, false);
        statusMessage = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_ERROR);
        siteService.updatePublishingStatusMessage(siteId, ERROR, statusMessage);
        throw err;
    }
}
Also used : DeploymentItemTO(org.craftercms.studio.api.v1.to.DeploymentItemTO) ArrayList(java.util.ArrayList) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) PublishRequest(org.craftercms.studio.api.v1.dal.PublishRequest) SimpleDateFormat(java.text.SimpleDateFormat) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) HashSet(java.util.HashSet)

Example 3 with PublishRequest

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

the class DeploymentServiceImpl method createDeleteItems.

private List<PublishRequest> createDeleteItems(String site, String environment, List<String> paths, String approver, ZonedDateTime scheduledDate, String submissionComment) throws SiteNotFoundException {
    List<PublishRequest> newItems = new ArrayList<PublishRequest>(paths.size());
    String packageId = UUID.randomUUID().toString();
    for (String path : paths) {
        if (contentService.contentExists(site, path)) {
            ContentItemTO contentItem = contentService.getContentItem(site, path, 0);
            if (!contentItem.isFolder()) {
                PublishRequest item = new PublishRequest();
                ItemMetadata metadata = objectMetadataManager.getProperties(site, path);
                item.setId(++CTED_AUTOINCREMENT);
                item.setSite(site);
                item.setEnvironment(environment);
                item.setPath(path);
                item.setScheduledDate(scheduledDate);
                item.setState(PublishRequest.State.READY_FOR_LIVE);
                item.setAction(PublishRequest.Action.DELETE);
                if (metadata != null) {
                    if (metadata.getRenamed() > 0) {
                        String oldPath = metadata.getOldUrl();
                        item.setOldPath(oldPath);
                    }
                    String commitId = metadata.getCommitId();
                    if (StringUtils.isNotEmpty(commitId) && contentRepositoryV2.commitIdExists(site, commitId)) {
                        item.setCommitId(commitId);
                    } else {
                        if (StringUtils.isNotEmpty(commitId)) {
                            logger.warn("Commit ID is NULL for content " + path + ". Was the git repo reset at some point?");
                        } else {
                            logger.warn("Commit ID " + commitId + " does not exist for content " + path + ". Was the git repo reset at some point?");
                        }
                        logger.info("Publishing content from HEAD for " + path);
                        item.setCommitId(contentRepository.getRepoLastCommitId(site));
                    }
                }
                String contentTypeClass = contentService.getContentTypeClass(site, path);
                item.setContentTypeClass(contentTypeClass);
                item.setUser(approver);
                item.setPackageId(packageId);
                item.setSubmissionComment(submissionComment);
                newItems.add(item);
                if (contentService.contentExists(site, path)) {
                    contentService.deleteContent(site, path, approver);
                    if (path.endsWith(FILE_SEPARATOR + DmConstants.INDEX_FILE)) {
                        deleteFolder(site, path.replace(FILE_SEPARATOR + DmConstants.INDEX_FILE, ""), approver);
                    }
                }
                String lastRepoCommitId = contentRepository.getRepoLastCommitId(site);
                if (StringUtils.isNotEmpty(lastRepoCommitId)) {
                    item.setCommitId(lastRepoCommitId);
                }
            } else {
                RepositoryItem[] children = contentRepository.getContentChildren(site, path);
                List<String> childPaths = new ArrayList<String>();
                for (RepositoryItem child : children) {
                    childPaths.add(child.path + FILE_SEPARATOR + child.name);
                }
                newItems.addAll(createDeleteItems(site, environment, childPaths, approver, scheduledDate, submissionComment));
                deleteFolder(site, path, approver);
            }
        }
    }
    return newItems;
}
Also used : RepositoryItem(org.craftercms.studio.api.v1.repository.RepositoryItem) ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ArrayList(java.util.ArrayList) FastArrayList(org.apache.commons.collections.FastArrayList) PublishRequest(org.craftercms.studio.api.v1.dal.PublishRequest) ItemMetadata(org.craftercms.studio.api.v1.dal.ItemMetadata)

Example 4 with PublishRequest

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

the class DeploymentServiceImpl method createCommitItems.

private List<PublishRequest> createCommitItems(String site, String environment, List<String> commitIds, ZonedDateTime scheduledDate, String approver, String comment) {
    List<PublishRequest> newItems = new ArrayList<PublishRequest>(commitIds.size());
    String packageId = UUID.randomUUID().toString();
    logger.debug("Get repository operations for each commit id and create publish request items");
    for (String commitId : commitIds) {
        logger.debug("Get repository operations for commit " + commitId);
        List<RepoOperation> operations = contentRepositoryV2.getOperationsFromDelta(site, commitId + PREVIOUS_COMMIT_SUFFIX, commitId);
        for (RepoOperation op : operations) {
            logger.debug("Creating publish request item: ");
            PublishRequest item = new PublishRequest();
            item.setId(++CTED_AUTOINCREMENT);
            item.setSite(site);
            item.setEnvironment(environment);
            item.setScheduledDate(scheduledDate);
            item.setState(PublishRequest.State.READY_FOR_LIVE);
            item.setCommitId(commitId);
            item.setUser(approver);
            item.setPackageId(packageId);
            item.setSubmissionComment(comment);
            switch(op.getAction()) {
                case CREATE:
                case COPY:
                    item.setPath(op.getPath());
                    item.setAction(PublishRequest.Action.NEW);
                    item.setContentTypeClass(contentService.getContentTypeClass(site, op.getPath()));
                    break;
                case UPDATE:
                    item.setPath(op.getPath());
                    item.setAction(PublishRequest.Action.UPDATE);
                    item.setContentTypeClass(contentService.getContentTypeClass(site, op.getPath()));
                    break;
                case DELETE:
                    item.setPath(op.getPath());
                    item.setAction(PublishRequest.Action.DELETE);
                    item.setContentTypeClass(contentService.getContentTypeClass(site, op.getPath()));
                    break;
                case MOVE:
                    item.setPath(op.getMoveToPath());
                    item.setOldPath(op.getPath());
                    item.setAction(PublishRequest.Action.MOVE);
                    item.setContentTypeClass(contentService.getContentTypeClass(site, op.getPath()));
                    break;
                default:
                    logger.error("Error: Unknown repo operation for site " + site + " operation: " + op.getAction());
                    continue;
            }
            logger.debug("\tPath: " + item.getPath() + " operation: " + item.getAction());
            newItems.add(item);
        }
    }
    logger.debug("Created " + newItems.size() + " publish request items for queue");
    return newItems;
}
Also used : RepoOperation(org.craftercms.studio.api.v2.dal.RepoOperation) ArrayList(java.util.ArrayList) FastArrayList(org.apache.commons.collections.FastArrayList) PublishRequest(org.craftercms.studio.api.v1.dal.PublishRequest)

Example 5 with PublishRequest

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

the class DeploymentServiceImpl method publishCommits.

@Override
@ValidateParams
public void publishCommits(@ValidateStringParam(name = "site") String site, @ValidateStringParam(name = "environment") String environment, List<String> commitIds, @ValidateStringParam(name = "comment") String comment) throws SiteNotFoundException, EnvironmentNotFoundException, CommitNotFoundException {
    if (!siteService.exists(site)) {
        throw new SiteNotFoundException();
    }
    Set<String> environments = getAllPublishedEnvironments(site);
    if (!environments.contains(environment)) {
        throw new EnvironmentNotFoundException();
    }
    if (!checkCommitIds(site, commitIds)) {
        throw new CommitNotFoundException();
    }
    logger.debug("Creating publish request items for queue for site " + site + " environment " + environment);
    List<PublishRequest> publishRequests = createCommitItems(site, environment, commitIds, ZonedDateTime.now(ZoneOffset.UTC), securityService.getCurrentUser(), comment);
    logger.debug("Insert publish request items to the queue");
    for (PublishRequest request : publishRequests) {
        publishRequestMapper.insertItemForDeployment(request);
    }
    logger.debug("Completed adding commits to publishing queue");
}
Also used : EnvironmentNotFoundException(org.craftercms.studio.api.v1.exception.EnvironmentNotFoundException) CommitNotFoundException(org.craftercms.studio.api.v1.exception.CommitNotFoundException) PublishRequest(org.craftercms.studio.api.v1.dal.PublishRequest) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Aggregations

PublishRequest (org.craftercms.studio.api.v1.dal.PublishRequest)14 ArrayList (java.util.ArrayList)10 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)8 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)5 FastArrayList (org.apache.commons.collections.FastArrayList)4 ItemMetadata (org.craftercms.studio.api.v1.dal.ItemMetadata)4 DeploymentException (org.craftercms.studio.api.v1.service.deployment.DeploymentException)4 DeploymentItemTO (org.craftercms.studio.api.v1.to.DeploymentItemTO)4 HashMap (java.util.HashMap)3 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)3 RetryingOperation (org.craftercms.studio.api.v2.annotation.RetryingOperation)3 HashSet (java.util.HashSet)2 CommitNotFoundException (org.craftercms.studio.api.v1.exception.CommitNotFoundException)2 EnvironmentNotFoundException (org.craftercms.studio.api.v1.exception.EnvironmentNotFoundException)2 RepositoryItem (org.craftercms.studio.api.v1.repository.RepositoryItem)2 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)2 PublishRequest (org.craftercms.studio.api.v2.dal.PublishRequest)2 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ZonedDateTime (java.time.ZonedDateTime)1