CCM NG/ccm-cms: Still working on the FolderManipulator
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4613 8810af33-2d31-482b-a856-94f89814c4df
Former-commit-id: d23818c69c
pull/2/head
parent
6a503b37a5
commit
159abff396
|
|
@ -60,6 +60,10 @@ import javax.transaction.Transactional;
|
|||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.Order;
|
||||
import javax.persistence.criteria.Path;
|
||||
import org.hibernate.tool.schema.internal.TargetFileImpl;
|
||||
import org.libreccm.categorization.ObjectNotAssignedToCategoryException;
|
||||
import org.libreccm.core.UnexpectedErrorException;
|
||||
import org.librecms.contentsection.FolderManager;
|
||||
|
||||
import static org.librecms.CmsConstants.*;
|
||||
|
||||
|
|
@ -77,6 +81,9 @@ public class FolderBrowserController {
|
|||
@Inject
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Inject
|
||||
private CcmObjectRepository ccmObjectRepo;
|
||||
|
||||
@Inject
|
||||
private CategoryManager categoryManager;
|
||||
|
||||
|
|
@ -104,6 +111,9 @@ public class FolderBrowserController {
|
|||
@Inject
|
||||
private FolderRepository folderRepo;
|
||||
|
||||
@Inject
|
||||
private FolderManager folderManager;
|
||||
|
||||
private Locale defaultLocale;
|
||||
|
||||
/**
|
||||
|
|
@ -636,7 +646,7 @@ public class FolderBrowserController {
|
|||
final List<String> invalidTargetIds = new ArrayList<>();
|
||||
invalidTargetIds.addAll(sourceFolderIds);
|
||||
invalidTargetIds.addAll(parentFolderIds);
|
||||
for(final List<String> subFolderIdList : subFolderIds) {
|
||||
for (final List<String> subFolderIdList : subFolderIds) {
|
||||
invalidTargetIds.addAll(subFolderIdList);
|
||||
}
|
||||
|
||||
|
|
@ -705,7 +715,7 @@ public class FolderBrowserController {
|
|||
}
|
||||
|
||||
final List<Folder> subFolders = new ArrayList<>();
|
||||
for(final Folder subFolder : folder.getSubFolders()) {
|
||||
for (final Folder subFolder : folder.getSubFolders()) {
|
||||
subFolders.add(subFolder);
|
||||
subFolders.addAll(findSubFolders(subFolder));
|
||||
}
|
||||
|
|
@ -713,4 +723,110 @@ public class FolderBrowserController {
|
|||
return subFolders;
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public void moveObjects(final Folder targetFolder,
|
||||
final String[] objectIds) {
|
||||
|
||||
Objects.requireNonNull(targetFolder);
|
||||
Objects.requireNonNull(objectIds);
|
||||
|
||||
for (final String objectId : objectIds) {
|
||||
if (objectId.startsWith(FOLDER_BROWSER_KEY_PREFIX_FOLDER)) {
|
||||
moveFolder(targetFolder,
|
||||
Long.parseLong(objectId.substring(
|
||||
FOLDER_BROWSER_KEY_PREFIX_FOLDER.length())));
|
||||
} else if (objectId.startsWith(FOLDER_BROWSER_KEY_PREFIX_ITEM)) {
|
||||
moveItem(targetFolder,
|
||||
Long.parseLong(objectId.substring(
|
||||
FOLDER_BROWSER_KEY_PREFIX_ITEM.length())));
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"ID '%s' does not start with '%s' or '%s'.",
|
||||
objectId,
|
||||
FOLDER_BROWSER_KEY_PREFIX_FOLDER,
|
||||
FOLDER_BROWSER_KEY_PREFIX_ITEM));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void moveFolder(final Folder targetFolder, final long folderId) {
|
||||
|
||||
Objects.requireNonNull(targetFolder);
|
||||
|
||||
final Folder folder = folderRepo.findById(folderId)
|
||||
.orElseThrow(() -> new IllegalArgumentException(String.format(
|
||||
"No folder with ID %d in the database. "
|
||||
+ "Where did that ID come from?",
|
||||
folderId)));
|
||||
|
||||
folderManager.moveFolder(folder, targetFolder);
|
||||
}
|
||||
|
||||
private void moveItem(final Folder targetFolder, final long itemId) {
|
||||
|
||||
Objects.requireNonNull(targetFolder);
|
||||
|
||||
final ContentItem item = itemRepo.findById(itemId)
|
||||
.orElseThrow(() -> new IllegalArgumentException(String.format(
|
||||
"No content item with ID %d in the database. "
|
||||
+ "Where did that ID come from?",
|
||||
itemId)));
|
||||
|
||||
itemManager.move(item, targetFolder);
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public void copyObjects(final Folder targetFolder,
|
||||
final String[] objectIds) {
|
||||
|
||||
Objects.requireNonNull(targetFolder);
|
||||
Objects.requireNonNull(objectIds);
|
||||
|
||||
for (final String objectId : objectIds) {
|
||||
if (objectId.startsWith(FOLDER_BROWSER_KEY_PREFIX_FOLDER)) {
|
||||
copyFolder(targetFolder,
|
||||
Long.parseLong(objectId.substring(
|
||||
FOLDER_BROWSER_KEY_PREFIX_FOLDER.length())));
|
||||
} else if (objectId.startsWith(FOLDER_BROWSER_KEY_PREFIX_ITEM)) {
|
||||
copyItem(targetFolder,
|
||||
Long.parseLong(objectId.substring(
|
||||
FOLDER_BROWSER_KEY_PREFIX_ITEM.length())));
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"ID '%s' does not start with '%s' or '%s'.",
|
||||
objectId,
|
||||
FOLDER_BROWSER_KEY_PREFIX_FOLDER,
|
||||
FOLDER_BROWSER_KEY_PREFIX_ITEM));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void copyFolder(final Folder targetFolder,
|
||||
final long folderId) {
|
||||
|
||||
Objects.requireNonNull(targetFolder);
|
||||
|
||||
final Folder folder = folderRepo.findById(folderId)
|
||||
.orElseThrow(() -> new IllegalArgumentException(String.format(
|
||||
"No folder with ID %d in the database. "
|
||||
+ "Where did that ID come from?",
|
||||
folderId)));
|
||||
|
||||
folderManager.copyFolder(folder, targetFolder);
|
||||
}
|
||||
|
||||
private void copyItem(final Folder targetFolder,
|
||||
final long itemId) {
|
||||
|
||||
Objects.requireNonNull(targetFolder);
|
||||
|
||||
final ContentItem item = itemRepo.findById(itemId)
|
||||
.orElseThrow(() -> new IllegalArgumentException(String.format(
|
||||
"No content item with ID %d in the database. "
|
||||
+ "Where did that ID come from?",
|
||||
itemId)));
|
||||
|
||||
itemManager.copy(item, targetFolder);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -203,55 +203,22 @@ public class FolderManipulator extends SimpleContainer implements
|
|||
return (String) state.getValue(actionParam);
|
||||
}
|
||||
|
||||
protected void moveItems(final Category target,
|
||||
final String[] itemIds) {
|
||||
protected void moveObjects(final Folder target, final String[] objectIds) {
|
||||
|
||||
for (String itemId : itemIds) {
|
||||
|
||||
changeItemParent(itemId, target);
|
||||
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
|
||||
final FolderBrowserController controller = cdiUtil.findBean(
|
||||
FolderBrowserController.class);
|
||||
|
||||
controller.moveObjects(target, objectIds);
|
||||
}
|
||||
|
||||
}
|
||||
protected void copyObjects(final Folder target, final String[] objectIds) {
|
||||
|
||||
private void changeItemParent(final String itemId,
|
||||
final Category newParent) {
|
||||
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
|
||||
final FolderBrowserController controller = cdiUtil.findBean(
|
||||
FolderBrowserController.class);
|
||||
|
||||
//ToDo
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
// final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
|
||||
// final ContentItemRepository itemRepo = cdiUtil.findBean(
|
||||
// ContentItemRepository.class);
|
||||
// final ContentItemManager itemManager = cdiUtil.findBean(
|
||||
// ContentItemManager.class);
|
||||
// final CategoryManager categoryManager = cdiUtil.findBean(
|
||||
// CategoryManager.class);
|
||||
// final ContentItem item = itemRepo.findById(itemId);
|
||||
// final Category itemFolder = itemManager.getItemFolders(item).
|
||||
// item.addCategory(newParent);
|
||||
// item.setParent(newParent);
|
||||
// item.save();
|
||||
}
|
||||
|
||||
protected void copyItems(final Category target,
|
||||
final String[] itemIds) {
|
||||
|
||||
//ToDo
|
||||
throw new UnsupportedOperationException();
|
||||
//
|
||||
// if (LOGGER.isDebugEnabled()) {
|
||||
// LOGGER.debug("Copying items " + Arrays.asList(itemIds) + " to "
|
||||
// + target);
|
||||
// }
|
||||
// for (BigDecimal itemId : itemIds) {
|
||||
//
|
||||
// final ContentItem source = retrieveSourceItem(itemId);
|
||||
//
|
||||
// final ContentItem newItem = source.copy(target, true);
|
||||
// Assert.isEqual(target, newItem.getParent());
|
||||
//
|
||||
// }
|
||||
controller.copyObjects(target, objectIds);
|
||||
}
|
||||
|
||||
// protected void publishItems(final BigDecimal[] itemIds) {
|
||||
|
|
@ -450,13 +417,13 @@ public class FolderManipulator extends SimpleContainer implements
|
|||
itemView.setVisible(state, true);
|
||||
targetSelector.setVisible(state, false);
|
||||
|
||||
final Category folder = targetSelector.getTarget(state);
|
||||
final Folder folder = targetSelector.getTarget(state);
|
||||
final String[] itemIds = getSources(state);
|
||||
|
||||
if (isCopy(state)) {
|
||||
copyItems(folder, itemIds);
|
||||
copyObjects(folder, itemIds);
|
||||
} else if (isMove(state)) {
|
||||
moveItems(folder, itemIds);
|
||||
moveObjects(folder, itemIds);
|
||||
}
|
||||
|
||||
reset(state);
|
||||
|
|
|
|||
|
|
@ -25,10 +25,10 @@ import com.arsdigita.cms.CMS;
|
|||
import com.arsdigita.ui.CcmObjectSelectionModel;
|
||||
|
||||
import org.libreccm.categorization.Category;
|
||||
import org.librecms.CmsConstants;
|
||||
import org.librecms.contentsection.ContentSection;
|
||||
import org.librecms.contentsection.Folder;
|
||||
|
||||
|
||||
/**
|
||||
* Keeps track of the selection of an item in a folder. The objects that are
|
||||
* selected by this model are all subclasses of {@link
|
||||
|
|
@ -50,7 +50,7 @@ public class FolderSelectionModel extends CcmObjectSelectionModel<Folder> {
|
|||
@Override
|
||||
public Long getSelectedKey(final PageState state) {
|
||||
// FIXME: this code will go away once parameter models support init listeners
|
||||
Long result = super.getSelectedKey(state);
|
||||
final Long result = super.getSelectedKey(state);
|
||||
if (result == null) {
|
||||
result = getRootFolderID(state);
|
||||
setSelectedKey(state, result);
|
||||
|
|
@ -63,10 +63,24 @@ public class FolderSelectionModel extends CcmObjectSelectionModel<Folder> {
|
|||
super.setSelectedKey(state, key);
|
||||
}
|
||||
|
||||
public void setSelectedKey(final PageState state, final String key) {
|
||||
if (!key.startsWith(CmsConstants.FOLDER_BROWSER_KEY_PREFIX_FOLDER)) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"The string '%s' does not contain a folder id "
|
||||
+ "(it does not start with '%s').",
|
||||
key,
|
||||
CmsConstants.FOLDER_BROWSER_KEY_PREFIX_FOLDER));
|
||||
}
|
||||
|
||||
final long keyAsLong = Long.parseLong(key.substring(
|
||||
CmsConstants.FOLDER_BROWSER_KEY_PREFIX_FOLDER.length()));
|
||||
setSelectedKey(state, keyAsLong);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the selection by resetting it to the root folder id.
|
||||
*
|
||||
* @param state represents the cuerent request.
|
||||
* @param state represents the current request.
|
||||
*/
|
||||
@Override
|
||||
public void clearSelection(final PageState state) {
|
||||
|
|
@ -93,6 +107,7 @@ public class FolderSelectionModel extends CcmObjectSelectionModel<Folder> {
|
|||
/**
|
||||
* Return true, since this selection model will always have a folder
|
||||
* selected in it
|
||||
*
|
||||
* @param state
|
||||
* @return
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import java.util.Collections;
|
|||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
|
|
@ -113,7 +114,8 @@ public class FolderManager {
|
|||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public Optional<Folder> getParentFolder(final Folder folder) {
|
||||
Objects.requireNonNull(folder);
|
||||
final Optional<Folder> theFolder = folderRepo.findById(folder.getObjectId());
|
||||
final Optional<Folder> theFolder = folderRepo.findById(folder.
|
||||
getObjectId());
|
||||
if (!theFolder.isPresent()) {
|
||||
throw new UnexpectedErrorException(String.format(
|
||||
"The folder %s should be in the database but is not.",
|
||||
|
|
@ -233,15 +235,8 @@ public class FolderManager {
|
|||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public void moveFolder(final Folder folder, final Folder target) {
|
||||
|
||||
if (folder
|
||||
== null) {
|
||||
throw new IllegalArgumentException("Can't move folder null");
|
||||
}
|
||||
|
||||
if (target == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't move a folder to folder null");
|
||||
}
|
||||
Objects.requireNonNull(folder, "Can't move folder null");
|
||||
Objects.requireNonNull(target, "Can't move a folder to folder null");
|
||||
|
||||
final FolderIsMovable status = folderIsMovable(folder, target);
|
||||
switch (status) {
|
||||
|
|
@ -251,8 +246,7 @@ public class FolderManager {
|
|||
final boolean sameName = target.getSubCategories()
|
||||
.stream()
|
||||
.anyMatch(subCategory -> folder.getName().equals(
|
||||
subCategory
|
||||
.getName()));
|
||||
subCategory.getName()));
|
||||
if (sameName) {
|
||||
final String name = String.format("%s_1", folder.getName());
|
||||
folder.setName(name);
|
||||
|
|
@ -392,6 +386,28 @@ public class FolderManager {
|
|||
return FolderIsMovable.YES;
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public void copyFolder(final Folder folder, final Folder target) {
|
||||
|
||||
Objects.requireNonNull(folder, "Can't move null to a folder.");
|
||||
Objects.requireNonNull(target, "Can't move a folder to null.");
|
||||
|
||||
final Folder copy = createFolder(folder.getName(), target);
|
||||
final List<ContentItem> items = folder.getObjects()
|
||||
.stream()
|
||||
.map(categorization -> categorization.getCategorizedObject())
|
||||
.filter(object -> object instanceof ContentItem)
|
||||
.map(object -> (ContentItem) object)
|
||||
.collect(Collectors.toList());
|
||||
for (final ContentItem item : items) {
|
||||
itemManager.copy(item, target);
|
||||
}
|
||||
|
||||
for(final Folder subFolder : folder.getSubFolders()) {
|
||||
copyFolder(subFolder, copy);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal helper method for checking if there any live items in a given
|
||||
* folder or its sub folders.
|
||||
|
|
@ -482,7 +498,7 @@ public class FolderManager {
|
|||
final List<Folder> folders = new ArrayList<>();
|
||||
if (getParentFolder(folder).isPresent()) {
|
||||
Optional<Folder> currentFolder = getParentFolder(folder);
|
||||
while(currentFolder.isPresent()) {
|
||||
while (currentFolder.isPresent()) {
|
||||
folders.add(currentFolder.get());
|
||||
currentFolder = getParentFolder(currentFolder.get());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -123,3 +123,5 @@ cms.contenttypes.ui.name=Name (URL fragment)
|
|||
cms.ui.content_section=Content Section:
|
||||
cms.ui.folderform.error.child.name_not_unique=The current folder already contains a child with the name {0}.
|
||||
cms.ui.folderform.error.parent.name_not_unique=The parent folder of the selected folder already contains a child with the name {0}.
|
||||
cms.ui.choose_target_folder=Choose target folder
|
||||
cms.ui.folder.copy=Copy {0} items from {1}
|
||||
|
|
|
|||
|
|
@ -122,3 +122,5 @@ cms.contenttypes.ui.name=Name (URL-Fragment)
|
|||
cms.ui.content_section=Content Section:
|
||||
cms.ui.folderform.error.child.name_not_unique=Der derzeit ausgew\u00e4hlte Ordner enth\u00e4lt bereits einen Ordner oder ein Dokument mit dem Namen {0}.
|
||||
cms.ui.folderform.error.parent.name_not_unique=Der \u00fcbergeordnete Ordner enth\u00e4lt bereits einen ein Objekt mit dem Namen {0}.
|
||||
cms.ui.choose_target_folder=Zielordner ausw\u00e4hlen
|
||||
cms.ui.folder.copy=Kopiere {0} Dokumente von {1} nach
|
||||
|
|
|
|||
|
|
@ -91,3 +91,5 @@ cms.contenttypes.ui.name=Name (URL fragment)
|
|||
cms.ui.content_section=Content Section:
|
||||
cms.ui.folderform.error.child.name_not_unique=The current folder already contains a child with the name {0}.
|
||||
cms.ui.folderform.error.parent.name_not_unique=The parent folder of the selected folder already contains a child with the name {0}.
|
||||
cms.ui.choose_target_folder=Choose target folder
|
||||
cms.ui.folder.copy=Copy {0} items from {1}
|
||||
|
|
|
|||
Loading…
Reference in New Issue