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, String path) throws SiteNotFoundException, ContentNotFoundException, ServiceLayerException {
Set<String> toRet = new HashSet<String>();
logger.debug("Resolving dependencies for content site: " + site + " path: " + path);
Map<String, Set<String>> dependencies = dependencyResolver.resolve(site, path);
List<DependencyEntity> dependencyEntities = new ArrayList<>();
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 site: " + site + " path: " + path);
deleteAllSourceDependencies(site, path);
logger.debug("Insert all extracted dependencies entries for site: " + site + " path: " + path);
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 + " path: " + path, e);
} finally {
generalLockService.unlock(lock);
}
}
return toRet;
}
use of org.craftercms.studio.api.v1.exception.SiteNotFoundException 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;
}
use of org.craftercms.studio.api.v1.exception.SiteNotFoundException 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");
}
use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.
the class DeploymentServiceImpl method enablePublishing.
@Override
@ValidateParams
public boolean enablePublishing(@ValidateStringParam(name = "site") String site, boolean enabled) throws SiteNotFoundException, AuthenticationException {
if (!siteService.exists(site)) {
throw new SiteNotFoundException();
}
if (!securityService.isSiteAdmin(securityService.getCurrentUser(), site)) {
throw new AuthenticationException();
}
boolean toRet = siteService.enablePublishing(site, enabled);
String message;
String status;
if (enabled) {
logger.info("Publishing started for site {0}", site);
if (publishingManager.isPublishingQueueEmpty(site)) {
message = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_DEFAULT);
status = READY;
} else {
message = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_QUEUED);
status = QUEUED;
}
} else {
logger.info("Publishing stopped for site {0}", site);
message = studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_STOPPED);
message = message.replace("{username}", securityService.getCurrentUser()).replace("{datetime}", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(DATE_PATTERN_WORKFLOW_WITH_TZ)));
status = STOPPED;
}
siteService.updatePublishingStatusMessage(site, status, message);
SiteFeed siteFeed = siteService.getSite(site);
AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
auditLog.setSiteId(siteFeed.getId());
if (enabled) {
auditLog.setOperation(OPERATION_START_PUBLISHER);
} else {
auditLog.setOperation(OPERATION_STOP_PUBLISHER);
}
auditLog.setActorId(securityService.getCurrentUser());
auditLog.setPrimaryTargetId(siteFeed.getSiteId());
auditLog.setPrimaryTargetType(TARGET_TYPE_SITE);
auditLog.setPrimaryTargetValue(siteFeed.getName());
auditServiceInternal.insertAuditLog(auditLog);
return toRet;
}
use of org.craftercms.studio.api.v1.exception.SiteNotFoundException in project studio by craftercms.
the class DeploymentServiceImpl method getDeploymentHistory.
@Override
@ValidateParams
public List<DmDeploymentTaskTO> getDeploymentHistory(@ValidateStringParam(name = "site") String site, @ValidateIntegerParam(name = "daysFromToday") int daysFromToday, @ValidateIntegerParam(name = "numberOfItems") int numberOfItems, @ValidateStringParam(name = "sort") String sort, boolean ascending, @ValidateStringParam(name = "filterType") String filterType) throws SiteNotFoundException {
// get the filtered list of attempts in a specific date range
ZonedDateTime toDate = ZonedDateTime.now(ZoneOffset.UTC);
ZonedDateTime fromDate = toDate.minusDays(daysFromToday);
List<DeploymentSyncHistory> deployReports = deploymentHistoryProvider.getDeploymentHistory(site, getEnvironmentNames(site), fromDate, toDate, dmFilterWrapper, filterType, numberOfItems);
List<DmDeploymentTaskTO> tasks = new ArrayList<DmDeploymentTaskTO>();
if (deployReports != null) {
int count = 0;
String timezone = servicesConfig.getDefaultTimezone(site);
Map<String, Set<String>> processedItems = new HashMap<String, Set<String>>();
for (int index = 0; index < deployReports.size() && count < numberOfItems; index++) {
DeploymentSyncHistory entry = deployReports.get(index);
String env = entry.getEnvironment();
if (!processedItems.containsKey(env)) {
processedItems.put(env, new HashSet<String>());
}
if (!processedItems.get(env).contains(entry.getPath())) {
ContentItemTO deployedItem = getDeployedItem(entry.getSite(), entry.getPath());
if (deployedItem != null) {
deployedItem.eventDate = entry.getSyncDate();
deployedItem.endpoint = entry.getTarget();
deployedItem.setUser(entry.getUser());
deployedItem.setEndpoint(entry.getEnvironment());
String deployedLabel = entry.getSyncDate().withZoneSameInstant(ZoneId.of(timezone)).format(ISO_OFFSET_DATE);
if (tasks.size() > 0) {
DmDeploymentTaskTO lastTask = tasks.get(tasks.size() - 1);
String lastDeployedLabel = lastTask.getInternalName();
if (lastDeployedLabel.equals(deployedLabel)) {
// add to the last task if it is deployed on the same day
lastTask.setNumOfChildren(lastTask.getNumOfChildren() + 1);
lastTask.getChildren().add(deployedItem);
} else {
tasks.add(createDeploymentTask(deployedLabel, deployedItem));
}
} else {
tasks.add(createDeploymentTask(deployedLabel, deployedItem));
}
processedItems.get(env).add(entry.getPath());
}
}
}
}
return tasks;
}
Aggregations