use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.
the class ContentServiceImpl method copyContent.
/**
* internal method copy that handles
* Get dependencies is already recursive
*/
protected String copyContent(String site, String fromPath, String toPath, Set<String> processedPaths) {
String retNewFileName = null;
String lifecycleOp = DmContentLifeCycleService.ContentLifeCycleOperation.COPY.toString();
String user = securityService.getCurrentUser();
String copyPath = null;
try {
Map<String, String> copyPathMap = constructNewPathforCutCopy(site, fromPath, toPath, true);
copyPath = copyPathMap.get("FILE_PATH");
String copyPathModifier = copyPathMap.get("MODIFIER");
String copyPathFileName = copyPathMap.get("FILE_NAME");
String copyPathFolder = copyPathMap.get("FILE_FOLDER");
String copyPathOnly = copyPath.substring(0, copyPath.lastIndexOf(FILE_SEPARATOR));
String copyFileName = copyPath.substring(copyPath.lastIndexOf(FILE_SEPARATOR) + 1);
if (!processedPaths.contains(copyPath)) {
ContentItemTO fromItem = getContentItem(site, fromPath, 0);
if (fromItem.isFolder()) {
createFolder(site, copyPathOnly, copyFileName);
// copy was successful, return the new name
retNewFileName = copyPath;
} else {
InputStream copyContent = null;
try {
String contentType = fromItem.getContentType();
InputStream fromContent = getContent(site, fromPath);
if (fromPath.endsWith(DmConstants.XML_PATTERN)) {
Document fromDocument = ContentUtils.convertStreamToXml(fromContent);
Map<String, String> fromPageIds = getContentIds(fromDocument);
logger.debug("copying file for site {0} from {1} to {2}, new name is {3}", site, fromPath, toPath, copyPath);
// come up with a new object ID and group ID for the object
Map<String, String> copyObjectIds = contentItemIdGenerator.getIds();
Map<String, String> copyDependencies = getCopyDependencies(site, fromPath, fromPath);
copyDependencies = getItemSpecificDependencies(site, fromPath, fromDocument, copyDependencies);
logger.debug("Calculated copy dependencies: {0}, {1}", fromPath, copyDependencies);
// Duplicate the children
for (String dependencyKey : copyDependencies.keySet()) {
String dependencyPath = copyDependencies.get(dependencyKey);
String copyDepPath = dependencyPath;
// try a simple substitution
copyDepPath = copyDepPath.replaceAll(fromPageIds.get(KEY_PAGE_ID), copyObjectIds.get(KEY_PAGE_ID));
copyDepPath = copyDepPath.replaceAll(fromPageIds.get(KEY_PAGE_GROUP_ID), copyObjectIds.get(KEY_PAGE_GROUP_ID));
ContentItemTO targetPathItem = getContentItem(site, copyDepPath);
if (targetPathItem != null && targetPathItem.isFolder()) {
copyDepPath = copyDepPath + FILE_SEPARATOR + FilenameUtils.getName(dependencyKey);
copyDepPath = copyDepPath.replaceAll(FILE_SEPARATOR + FILE_SEPARATOR, FILE_SEPARATOR);
} else if (!copyDepPath.endsWith(DmConstants.XML_PATTERN)) {
copyDepPath = ContentUtils.getParentUrl(copyDepPath);
}
logger.debug("Translated dependency path from {0} to {1}", dependencyPath, copyDepPath);
String newCopyDepthPath = copyContent(site, dependencyKey, copyDepPath, processedPaths);
fromDocument = replaceCopyDependency(fromDocument, dependencyKey, newCopyDepthPath);
}
// update the file name / folder values
Document copyDocument = updateContentOnCopy(fromDocument, copyPathFileName, copyPathFolder, copyObjectIds, copyPathModifier);
copyContent = ContentUtils.convertDocumentToStream(copyDocument, CONTENT_ENCODING);
}
// This code is very similar to what is in writeContent. Consolidate this code?
Map<String, String> params = new HashMap<String, String>();
params.put(DmConstants.KEY_SITE, site);
params.put(DmConstants.KEY_PATH, copyPathOnly);
params.put(DmConstants.KEY_FILE_NAME, copyFileName);
params.put(DmConstants.KEY_USER, user);
params.put(DmConstants.KEY_CONTENT_TYPE, contentType);
params.put(DmConstants.KEY_CREATE_FOLDERS, "true");
params.put(DmConstants.KEY_EDIT, "true");
params.put(DmConstants.KEY_ACTIVITY_TYPE, "false");
params.put(DmConstants.KEY_SKIP_CLEAN_PREVIEW, "true");
params.put(DmConstants.KEY_COPIED_CONTENT, "true");
params.put(DmConstants.CONTENT_LIFECYCLE_OPERATION, lifecycleOp);
String id = site + ":" + copyPathOnly + ":" + copyFileName + ":" + contentType;
// processContent will close the input stream
if (copyFileName.endsWith(DmConstants.XML_PATTERN)) {
processContent(id, copyContent, true, params, DmConstants.CONTENT_CHAIN_FORM);
} else {
processContent(id, fromContent, false, params, DmConstants.CONTENT_CHAIN_ASSET);
}
ItemState itemState = objectStateService.getObjectState(site, copyPath);
if (itemState == null) {
ContentItemTO copyItem = getContentItem(site, copyPath, 0);
objectStateService.insertNewEntry(site, copyItem);
objectStateService.setSystemProcessing(site, copyPath, false);
}
// copy was successful, return the new name
retNewFileName = copyPath;
// track that we already copied so we don't follow a circular dependency
processedPaths.add(copyPath);
} catch (ContentNotFoundException eContentNotFound) {
logger.debug("Content not found while copying content for site {0} from {1} to {2}," + " new name is {3}", eContentNotFound, site, fromPath, toPath, copyPath);
} catch (DocumentException eParseException) {
logger.error("General Error while copying content for site {0} from {1} to {2}," + " new name is {3}", eParseException, site, fromPath, toPath, copyPath);
} catch (CryptoException e) {
logger.error("Unexpected Error while copying content for site {0} from {1} to {2}," + " new name is {3}", e, site, fromPath, toPath, copyPath);
} finally {
IOUtils.closeQuietly(copyContent);
}
}
} else {
// no need to process
retNewFileName = copyPath;
}
} catch (ServiceLayerException eServiceLayerException) {
logger.info("General Error while copying content for site {0} from {1} to {2}, new name is {3}", eServiceLayerException, site, fromPath, toPath, copyPath);
}
return retNewFileName;
}
use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.
the class ObjectStateServiceImpl method transition.
@RetryingOperation
@Override
@ValidateParams
public void transition(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, TransitionEvent event) {
String itemPath = FilenameUtils.normalize(path, true);
String lockKey = site + ":" + path;
generalLockService.lock(lockKey);
try {
Map<String, String> params = new HashMap<String, String>();
params.put("site", site);
params.put("path", itemPath);
ItemState currentState = itemStateMapper.getObjectStateBySiteAndPath(params);
State nextState = null;
if (currentState == null) {
logger.debug("Preforming transition event " + event.name() + " on object " + lockKey + " without current state");
switch(event) {
case SAVE:
nextState = State.NEW_UNPUBLISHED_UNLOCKED;
break;
case SAVE_FOR_PREVIEW:
nextState = State.NEW_UNPUBLISHED_LOCKED;
break;
default:
nextState = State.NEW_UNPUBLISHED_UNLOCKED;
}
} else {
logger.debug("Preforming transition event " + event + " on object " + lockKey + " with " + currentState.getState() + " state");
State currentStateValue = State.valueOf(currentState.getState());
nextState = transitionTable[currentStateValue.ordinal()][event.ordinal()];
}
if (currentState == null) {
ItemState newEntry = new ItemState();
newEntry.setObjectId(UUID.randomUUID().toString());
newEntry.setSite(site);
newEntry.setPath(itemPath);
newEntry.setSystemProcessing(0);
newEntry.setState(nextState.name());
itemStateMapper.insertEntry(newEntry);
} else if (nextState.toString() != currentState.getState() && nextState != State.NOOP) {
currentState.setState(nextState.name());
itemStateMapper.setObjectState(currentState);
} else if (nextState == State.NOOP) {
logger.warn("Transition not defined for event " + event.name() + " and current state " + currentState.getState() + " [object id: " + currentState.getObjectId() + "]");
}
} catch (Exception e) {
logger.error("Transition not defined for event", e);
} finally {
generalLockService.unlock(lockKey);
}
logger.debug("Transition finished for " + event.name() + " on object " + lockKey);
}
use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.
the class ObjectStateServiceImpl method setObjectState.
@RetryingOperation
@Override
@ValidateParams
public String setObjectState(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, @ValidateStringParam(name = "state") String state, boolean systemProcessing) {
path = FilenameUtils.normalize(path, true);
Map<String, String> params = new HashMap<String, String>();
params.put("site", site);
params.put("path", path);
ItemState objectState = itemStateMapper.getObjectStateBySiteAndPath(params);
if (objectState == null) {
insertNewEntry(site, path);
objectState = itemStateMapper.getObjectStateBySiteAndPath(params);
}
objectState.setState(state);
objectState.setSystemProcessing(systemProcessing ? 1 : 0);
itemStateMapper.setObjectState(objectState);
return "Success";
}
use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.
the class ObjectStateServiceImpl method isUpdated.
@Override
@ValidateParams
public boolean isUpdated(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path) {
path = FilenameUtils.normalize(path, true);
ItemState state = getObjectState(site, path);
if (state != null) {
return State.isUpdated(State.valueOf(state.getState()));
} else {
return false;
}
}
use of org.craftercms.studio.api.v1.dal.ItemState in project studio by craftercms.
the class ObjectStateServiceImpl method getChangeSetForSubtree.
@Override
@ValidateParams
public List<String> getChangeSetForSubtree(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("site", site);
params.put("path", path);
params.put("likepath", path + (path.endsWith(FILE_SEPARATOR) ? "" : FILE_SEPARATOR) + "%");
params.put("states", State.CHANGE_SET_STATES);
List<ItemState> result = itemStateMapper.getChangeSetForSubtree(params);
List<String> toRet = new ArrayList<String>();
for (ItemState state : result) {
toRet.add(state.getPath());
}
return toRet;
}
Aggregations