From 4f6cc9382bc1e47b4fa499f35ccf1e4cf452d80a Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 20 Jul 2017 17:13:46 +0000 Subject: [PATCH] CCM NG/ccm-cms: Some bugfixes for Assets git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4882 8810af33-2d31-482b-a856-94f89814c4df --- .../arsdigita/cms/ui/assets/AssetForm.java | 60 +++++++++++-------- .../cms/ui/assets/forms/BinaryAssetForm.java | 30 ++++++---- .../cms/ui/assets/forms/ImageForm.java | 47 ++++++++------- .../org/librecms/contentsection/Asset.java | 34 +++++++++++ .../contentsection/AssetRepository.java | 53 +++++++++++++--- .../contentsection/ContentItemRepository.java | 13 ++-- .../org/librecms/CmsResources.properties | 2 + .../org/librecms/CmsResources_de.properties | 2 + .../org/librecms/CmsResources_fr.properties | 2 + .../bebop/parameters/LongParameter.java | 32 +++++----- 10 files changed, 188 insertions(+), 87 deletions(-) diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetForm.java index 3f4577c88..ca802d1c2 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetForm.java @@ -91,7 +91,7 @@ public abstract class AssetForm extends Form implements FormInitListener, } private void initComponents() { - + showLocalePanel = new BoxPanel(BoxPanel.HORIZONTAL); final Label showLocaleLabel = new Label(new PrintListener() { @@ -165,12 +165,13 @@ public abstract class AssetForm extends Form implements FormInitListener, showLocaleSubmit = new Submit(new GlobalizedMessage( "cms.ui.asset.show_locale", CmsConstants.CMS_BUNDLE)) { - - @Override - public boolean isVisible(final PageState state) { - return getSelectedAsset(state).isPresent(); - } - }; + + @Override + public boolean isVisible(final PageState state) { + return getSelectedAsset(state).isPresent(); + } + + }; showLocalePanel.add(showLocaleLabel); showLocalePanel.add(showLocaleSelect); showLocalePanel.add(showLocaleSubmit); @@ -260,14 +261,13 @@ public abstract class AssetForm extends Form implements FormInitListener, return Optional.empty(); } else { final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final AssetRepository assetRepo = cdiUtil.findBean( - AssetRepository.class); + final AssetRepository assetRepo = cdiUtil + .findBean(AssetRepository.class); final Asset asset = assetRepo - .findById(selectionModel.getSelectedKey(state)) + .findById((long) selectionModel.getSelectedKey(state)) .orElseThrow(() -> new IllegalArgumentException(String. - format( - "No asset with ID %d in the database.", - selectionModel.getSelectedKey(state)))); + format("No asset with ID %d in the database.", + selectionModel.getSelectedKey(state)))); return Optional.of(asset); } } @@ -312,8 +312,19 @@ public abstract class AssetForm extends Form implements FormInitListener, } } - protected abstract void initForm(final PageState state, - final Optional selectedAsset); + protected void initForm(final PageState state, + final Optional selectedAsset) { + + if (selectedAsset.isPresent()) { + + title.setValue(state, + selectedAsset + .get() + .getTitle() + .getValue(getSelectedLocale(state))); + showLocale(state); + } + } @Override public void process(final FormSectionEvent event) @@ -326,16 +337,17 @@ public abstract class AssetForm extends Form implements FormInitListener, if (showLocaleSubmit.isSelected(state)) { final Optional selectedAsset = getSelectedAsset(state); + initForm(state, selectedAsset); - if (selectedAsset.isPresent()) { - - title.setValue(state, - selectedAsset - .get() - .getTitle() - .getValue(getSelectedLocale(state))); - showLocale(state); - } +// if (selectedAsset.isPresent()) { +// +// title.setValue(state, +// selectedAsset +// .get() +// .getTitle() +// .getValue(getSelectedLocale(state))); +// showLocale(state); +// } return; } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BinaryAssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BinaryAssetForm.java index 5163a3cff..eddbeb513 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BinaryAssetForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BinaryAssetForm.java @@ -95,7 +95,7 @@ public abstract class BinaryAssetForm extends AssetForm { panel.add(fileUpload); add(panel); - + setEncType(CmsConstants.FORM_ENCTYPE_MULTIPART); } @@ -103,6 +103,8 @@ public abstract class BinaryAssetForm extends AssetForm { protected void initForm(final PageState state, final Optional selectedAsset) { + super.initForm(state, selectedAsset); + if (selectedAsset.isPresent()) { if (!(selectedAsset.get() instanceof BinaryAsset)) { @@ -126,7 +128,7 @@ public abstract class BinaryAssetForm extends AssetForm { mimeType.setText("-"); size.setText("-"); } else { - + fileName.setText(binaryAsset.getFileName()); mimeType.setText(binaryAsset.getMimeType().toString()); size.setText(Long.toString(binaryAsset.getSize())); @@ -211,18 +213,20 @@ public abstract class BinaryAssetForm extends AssetForm { throws FormProcessException { final File file = fileUpload.getFile(event); - final Path path = file.toPath(); - final byte[] data; - try { - data = Files.readAllBytes(path); - } catch (IOException ex) { - throw new FormProcessException(ex); - } - binaryAsset.setData(data); - binaryAsset.setFileName(fileUpload.getFileName(event)); - binaryAsset.setSize(data.length); + if (file != null) { + final Path path = file.toPath(); + final byte[] data; + try { + data = Files.readAllBytes(path); + } catch (IOException ex) { + throw new FormProcessException(ex); + } + binaryAsset.setData(data); + binaryAsset.setFileName(fileUpload.getFileName(event)); + binaryAsset.setSize(data.length); - binaryAsset.setMimeType(fileUpload.getMimeType(event)); + binaryAsset.setMimeType(fileUpload.getMimeType(event)); + } } } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ImageForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ImageForm.java index bc88aff2c..d7f02130e 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ImageForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ImageForm.java @@ -26,10 +26,10 @@ import com.arsdigita.bebop.form.TextField; import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.cms.ui.assets.AssetSearchWidget; import com.arsdigita.globalization.GlobalizedMessage; + import org.libreccm.cdi.utils.CdiUtil; import org.librecms.CmsConstants; import org.librecms.assets.BinaryAsset; -import org.librecms.assets.ExternalAudioAsset; import org.librecms.assets.Image; import org.librecms.assets.LegalMetadata; import org.librecms.contentsection.Asset; @@ -54,31 +54,35 @@ public class ImageForm extends BinaryAssetForm { @Override protected void addWidgets() { + super.addWidgets(); + width = new TextField("width-text"); height = new TextField("height-text"); - assetSearchWidget = new AssetSearchWidget("legal-metadata", LegalMetadata.class); + assetSearchWidget = new AssetSearchWidget("legal-metadata", + LegalMetadata.class); add(new Label(new GlobalizedMessage( - "cms.ui.assets.image.width.label", - CmsConstants.CMS_BUNDLE + "cms.ui.assets.image.width.label", + CmsConstants.CMS_BUNDLE ))); add(width); add(new Label(new GlobalizedMessage( - "cms.ui.assets.image.height.label", - CmsConstants.CMS_BUNDLE + "cms.ui.assets.image.height.label", + CmsConstants.CMS_BUNDLE ))); add(height); add(new Label(new GlobalizedMessage( - "cms.ui.assets.image.legal_metadata.label", - CmsConstants.CMS_BUNDLE + "cms.ui.assets.image.legal_metadata.label", + CmsConstants.CMS_BUNDLE ))); add(assetSearchWidget); } @Override - protected void initForm(PageState state, Optional selectedAsset) { + protected void initForm(final PageState state, + final Optional selectedAsset) { super.initForm(state, selectedAsset); @@ -87,12 +91,14 @@ public class ImageForm extends BinaryAssetForm { Image image = (Image) selectedAsset.get(); width.setValue(state, - Long.toString(image.getWidth())); + Long.toString(image.getWidth())); height.setValue(state, - Long.toString(image.getHeight())); + Long.toString(image.getHeight())); final LegalMetadata legalMetadata = image - .getLegalMetadata(); - if (legalMetadata != null) { + .getLegalMetadata(); + if (legalMetadata == null) { + assetSearchWidget.setValue(state, null); + } else { assetSearchWidget.setValue(state, legalMetadata.getObjectId()); } } @@ -100,7 +106,7 @@ public class ImageForm extends BinaryAssetForm { @Override protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { + throws FormProcessException { final Image image = (Image) super.createAsset(event); @@ -116,7 +122,7 @@ public class ImageForm extends BinaryAssetForm { @Override protected void updateAsset(final Asset asset, final FormSectionEvent event) - throws FormProcessException { + throws FormProcessException { super.updateAsset(asset, event); @@ -137,12 +143,12 @@ public class ImageForm extends BinaryAssetForm { if (legalMetadataId != null) { final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); final AssetRepository assetRepo = cdiUtil.findBean( - AssetRepository.class); + AssetRepository.class); final LegalMetadata legalMetadata = (LegalMetadata) assetRepo - .findById(legalMetadataId) - .orElseThrow(() -> new IllegalArgumentException(String.format( - "No LegalMetadata asset with ID %d in the database.", - legalMetadataId))); + .findById(legalMetadataId) + .orElseThrow(() -> new IllegalArgumentException(String.format( + "No LegalMetadata asset with ID %d in the database.", + legalMetadataId))); image.setLegalMetadata(legalMetadata); } @@ -152,4 +158,5 @@ public class ImageForm extends BinaryAssetForm { protected BinaryAsset createBinaryAsset(final PageState state) { return new Image(); } + } diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/Asset.java b/ccm-cms/src/main/java/org/librecms/contentsection/Asset.java index 80170930d..1335cff34 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/Asset.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/Asset.java @@ -57,6 +57,39 @@ import static org.librecms.CmsConstants.*; @Inheritance(strategy = InheritanceType.JOINED) @Audited @NamedQueries({ + @NamedQuery( + name = "Asset.findById", + query = "SELECT DISTINCT a " + + "FROM Asset a " + + "LEFT JOIN a.permissions p " + + "WHERE a.objectId = :assetId " + + "AND (" + + " (" + + " p.grantee IN :roles " + + " AND p.grantedPrivilege = " + + " '" + AssetPrivileges.VIEW + "' " + + " ) " + + " OR true = :isSystemUser OR true = :isAdmin" + + ")" + ) + , +@NamedQuery( + name = "Asset.findByIdAndType", + query = "SELECT DISTINCT a " + + "FROM Asset a " + + "LEFT JOIN a.permissions p " + + "WHERE a.objectId = :assetId " + + "AND TYPE(a) = :type " + + "AND (" + + " (" + + " p.grantee IN :roles " + + " AND p.grantedPrivilege = " + + " '" + AssetPrivileges.VIEW + "' " + + " )" + + " OR true = :isSystemUser OR true = :isAdmin" + + ")" + ) + , @NamedQuery( name = "Asset.findByUuid", query = "SELECT DISTINCT a " @@ -357,6 +390,7 @@ import static org.librecms.CmsConstants.*; + " ) " + " OR true = :isSystemUser OR true = :isAdmin" + ")") + }) public class Asset extends CcmObject { diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/AssetRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/AssetRepository.java index 803edf9e6..3d3209571 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/AssetRepository.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/AssetRepository.java @@ -56,10 +56,10 @@ public class AssetRepository @Inject private Shiro shiro; - + @Inject private PermissionChecker permissionChecker; - + @Inject private EntityManager entityManager; @@ -101,6 +101,44 @@ public class AssetRepository } } + @Transactional(Transactional.TxType.REQUIRED) + public Optional findById(final long assetId) { + + final TypedQuery query = getEntityManager() + .createNamedQuery("Asset.findById", Asset.class) + .setParameter("assetId", assetId); + setAuthorizationParameters(query); + + try { + return Optional.of(query.getSingleResult()); + } catch (NoResultException ex) { + return Optional.empty(); + } + } + + @Transactional(Transactional.TxType.REQUIRED) + @SuppressWarnings("unchecked") + public Optional findById(final long assetId, + final Class type) { + + final TypedQuery query = getEntityManager() + .createNamedQuery("assetId", Asset.class) + .setParameter("assetId", assetId) + .setParameter("type", type); + setAuthorizationParameters(query); + + try { + final Asset result = query.getSingleResult(); + if (result.getClass().isAssignableFrom(type)) { + return Optional.of((T) query.getSingleResult()); + } else { + return Optional.empty(); + } + } catch (NoResultException ex) { + return Optional.empty(); + } + } + @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) @Override @@ -161,7 +199,7 @@ public class AssetRepository */ @Transactional(Transactional.TxType.REQUIRED) public Optional findByUuid(final String uuid) { - + final TypedQuery query = entityManager .createNamedQuery("Asset.findByUuid", Asset.class); query.setParameter("uuid", uuid); @@ -362,7 +400,7 @@ public class AssetRepository "Asset.countInFolder", Long.class); query.setParameter("folder", folder); setAuthorizationParameters(query); - + return query.getSingleResult(); } @@ -513,7 +551,7 @@ public class AssetRepository } private void setAuthorizationParameters(final TypedQuery query) { - + final Optional user = shiro.getUser(); final List roles; if (user.isPresent()) { @@ -526,8 +564,7 @@ public class AssetRepository } else { roles = Collections.emptyList(); } - - + final boolean isSystemUser = shiro.isSystemUser(); final boolean isAdmin = permissionChecker.isPermitted("*"); @@ -535,5 +572,5 @@ public class AssetRepository query.setParameter("isSystemUser", isSystemUser); query.setParameter("isAdmin", isAdmin); } - + } diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java index ee2069adf..7bde65ee4 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java @@ -88,13 +88,13 @@ public class ContentItemRepository @Inject private WorkflowRepository workflowRepo; - + @Inject private TaskRepository taskRepo; - + @Inject private TaskManager taskManager; - + @Inject private PermissionChecker permissionChecker; @@ -134,8 +134,7 @@ public class ContentItemRepository public Optional findById(final long itemId) { final TypedQuery query = getEntityManager() - .createNamedQuery("ContentItem.findById", - ContentItem.class); + .createNamedQuery("ContentItem.findById", ContentItem.class); query.setParameter("objectId", itemId); setAuthorizationParameters(query); @@ -499,10 +498,10 @@ public class ContentItemRepository removeCategoryFromItem(item, category); } - + if (draft.getWorkflow() != null) { final Workflow workflow = draft.getWorkflow(); - for(final Task task : workflow.getTasks()) { + for (final Task task : workflow.getTasks()) { taskManager.removeTask(workflow, task); } workflowRepo.delete(workflow); diff --git a/ccm-cms/src/main/resources/org/librecms/CmsResources.properties b/ccm-cms/src/main/resources/org/librecms/CmsResources.properties index 90b30178c..f8c4c2a57 100644 --- a/ccm-cms/src/main/resources/org/librecms/CmsResources.properties +++ b/ccm-cms/src/main/resources/org/librecms/CmsResources.properties @@ -383,3 +383,5 @@ image_step.label=Images related_info_step.label=Related information image_step.description=Attach images related_info_step_description=Add related information +cms.ui.authoring.file_upload.auto_detect=(Auto-Detect) +cms.ui.upload_new_content=Upload new content diff --git a/ccm-cms/src/main/resources/org/librecms/CmsResources_de.properties b/ccm-cms/src/main/resources/org/librecms/CmsResources_de.properties index 50ba2452e..26c6475b0 100644 --- a/ccm-cms/src/main/resources/org/librecms/CmsResources_de.properties +++ b/ccm-cms/src/main/resources/org/librecms/CmsResources_de.properties @@ -380,3 +380,5 @@ image_step.label=Bilder related_info_step.label=Weiterf\u00fchrende Informationen image_step.description=Bilder hinzuf\u00fcgen related_info_step_description=Weiterf\u00fchrende Informationen hinzuf\u00fcgen +cms.ui.authoring.file_upload.auto_detect=(Automatisch erkennen) +cms.ui.upload_new_content=Neuen Inhalt hochladen diff --git a/ccm-cms/src/main/resources/org/librecms/CmsResources_fr.properties b/ccm-cms/src/main/resources/org/librecms/CmsResources_fr.properties index 027202cc4..e64423fa4 100644 --- a/ccm-cms/src/main/resources/org/librecms/CmsResources_fr.properties +++ b/ccm-cms/src/main/resources/org/librecms/CmsResources_fr.properties @@ -339,3 +339,5 @@ image_step.label=Images related_info_step.label=Related information image_step.description=Attach images related_info_step_description=Add related information +cms.ui.authoring.file_upload.auto_detect=(Auto-Detect) +cms.ui.upload_new_content=Upload new content diff --git a/ccm-core/src/main/java/com/arsdigita/bebop/parameters/LongParameter.java b/ccm-core/src/main/java/com/arsdigita/bebop/parameters/LongParameter.java index 5d25f9f39..74a8b2da9 100755 --- a/ccm-core/src/main/java/com/arsdigita/bebop/parameters/LongParameter.java +++ b/ccm-core/src/main/java/com/arsdigita/bebop/parameters/LongParameter.java @@ -18,34 +18,36 @@ */ package com.arsdigita.bebop.parameters; - /** - * A class that represents the model for number form parameters. + * A class that represents the model for number form parameters. * - * @author Randy Graebner (randyg@alum.mit.edu) - * @version $Id$ + * @author Randy Graebner + * @author Jens Pelzetter */ - public class LongParameter extends NumberParameter { - public LongParameter(String name) { + public LongParameter(final String name) { super(name); } @Override - public Object unmarshal(String encoded) { - try { - return new Long(encoded); - } - catch (NumberFormatException e) { - throw new IllegalArgumentException(getName() + " should be a " + - "Long Number, but is '" + - encoded + "'"); + public Object unmarshal(final String encoded) { + if (encoded == null || encoded.isEmpty()) { + return null; + } else { + try { + return new Long(encoded); + } catch (NumberFormatException ex) { + throw new IllegalArgumentException(String + .format("%s should be a Long Number, but is '%s'", + getName(), + encoded)); + } } } @Override - public Class getValueClass() { + public Class getValueClass() { return Long.class; }