CCM NG: Refactorin of asset forms

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@6152 8810af33-2d31-482b-a856-94f89814c4df

Former-commit-id: 4de5725f7c
pull/2/head
jensp 2019-07-25 13:03:00 +00:00
parent 19b245a313
commit 4feb3c9ca2
16 changed files with 752 additions and 463 deletions

View File

@ -18,20 +18,15 @@
*/ */
package com.arsdigita.cms.ui.assets.forms; package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label; import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.cms.ui.assets.AssetPane;
import com.arsdigita.cms.ui.assets.AssetSearchWidget; import com.arsdigita.cms.ui.assets.AssetSearchWidget;
import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.globalization.GlobalizedMessage;
import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.CmsConstants; import org.librecms.CmsConstants;
import org.librecms.assets.AudioAsset; import org.librecms.assets.AudioAsset;
import org.librecms.assets.LegalMetadata; import org.librecms.assets.LegalMetadata;
import org.librecms.contentsection.Asset;
import org.librecms.contentsection.AssetRepository;
import java.util.Map; import java.util.Map;

View File

@ -18,6 +18,7 @@
*/ */
package com.arsdigita.cms.ui.assets.forms; package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label; import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.event.FormSectionEvent;
@ -25,16 +26,12 @@ import com.arsdigita.cms.ui.assets.AssetPane;
import com.arsdigita.cms.ui.assets.AssetSearchWidget; import com.arsdigita.cms.ui.assets.AssetSearchWidget;
import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.globalization.GlobalizedMessage;
import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.CmsConstants; import org.librecms.CmsConstants;
import org.librecms.assets.Bookmark;
import org.librecms.assets.ExternalAudioAsset; import org.librecms.assets.ExternalAudioAsset;
import org.librecms.assets.LegalMetadata; import org.librecms.assets.LegalMetadata;
import org.librecms.contentsection.Asset;
import org.librecms.contentsection.AssetRepository;
import java.util.Objects; import java.util.HashMap;
import java.util.Optional; import java.util.Map;
/** /**
* *
@ -64,17 +61,21 @@ public class ExternalAudioAssetForm
@Override @Override
protected void initForm(final PageState state, protected void initForm(final PageState state,
final Optional<ExternalAudioAsset> selectedAsset) { final Map<String, Object> data) {
super.initForm(state, selectedAsset);
if (selectedAsset.isPresent()) { super.initForm(state, data);
final ExternalAudioAsset externalAudioAsset
= (ExternalAudioAsset) selectedAsset.get();
final LegalMetadata legalMetadata = externalAudioAsset final Long selectedAssetId = getSelectedAssetId(state);
.getLegalMetadata();
if (legalMetadata != null) { if (selectedAssetId != null) {
assetSearchWidget.setValue(state, legalMetadata.getObjectId());
if (data.containsKey(
ExternalAudioAssetFormController.LEGAL_METADATA_ID)) {
final long legalMetadataId = (long) data
.get(ExternalAudioAssetFormController.LEGAL_METADATA_ID);
assetSearchWidget.setValue(state, legalMetadataId);
} }
} }
} }
@ -84,60 +85,20 @@ public class ExternalAudioAssetForm
return ExternalAudioAsset.class; return ExternalAudioAsset.class;
} }
// @Override
// protected Asset createAsset(final FormSectionEvent event)
// throws FormProcessException {
//
// Objects.requireNonNull(event);
//
// final PageState state = event.getPageState();
//
// final ExternalAudioAsset externalAudioAsset = new ExternalAudioAsset();
//
// updateData((Bookmark) externalAudioAsset, state);
// updateData(externalAudioAsset, state);
//
// return externalAudioAsset;
// }
protected void updateData(final ExternalAudioAsset externalAudioAsset,
final PageState state) {
final Long legalMetadataId = (Long) assetSearchWidget.getValue(state);
if (legalMetadataId != null) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final AssetRepository assetRepo = cdiUtil.findBean(
AssetRepository.class);
final LegalMetadata legalMetadata = (LegalMetadata) assetRepo
.findById(legalMetadataId)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No LegalMetadata asset with ID %d in the database.",
legalMetadataId)));
externalAudioAsset.setLegalMetadata(legalMetadata);
}
}
@Override @Override
protected void updateAsset(final Asset asset, protected Map<String, Object> collectData(final FormSectionEvent event)
final FormSectionEvent event) { throws FormProcessException {
Objects.requireNonNull(asset);
Objects.requireNonNull(event);
final PageState state = event.getPageState(); final PageState state = event.getPageState();
if (!(asset instanceof ExternalAudioAsset)) { final Map<String, Object> data = new HashMap<>();
throw new IllegalArgumentException(String.format( if (assetSearchWidget.getValue(state) != null) {
"Provided asset is not an instance of class (or sub class of) "
+ "'%s' but is an instance of class '%s'", data.put(ExternalAudioAssetFormController.LEGAL_METADATA_ID,
ExternalAudioAsset.class.getName(), assetSearchWidget.getValue(state));
asset.getClass().getName()));
} }
final ExternalAudioAsset ExternalAudioAsset = (ExternalAudioAsset) asset; return data;
updateData((Bookmark) ExternalAudioAsset, state);
updateData(ExternalAudioAsset, state);
} }
} }

View File

@ -18,6 +18,7 @@
*/ */
package com.arsdigita.cms.ui.assets.forms; package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label; import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.event.FormSectionEvent;
@ -25,17 +26,13 @@ import com.arsdigita.cms.ui.assets.AssetPane;
import com.arsdigita.cms.ui.assets.AssetSearchWidget; import com.arsdigita.cms.ui.assets.AssetSearchWidget;
import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.globalization.GlobalizedMessage;
import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.CmsConstants; import org.librecms.CmsConstants;
import org.librecms.assets.Bookmark;
import java.util.Objects;
import java.util.Optional;
import org.librecms.assets.ExternalVideoAsset; import org.librecms.assets.ExternalVideoAsset;
import org.librecms.assets.LegalMetadata; import org.librecms.assets.LegalMetadata;
import org.librecms.contentsection.Asset;
import org.librecms.contentsection.AssetRepository; import java.util.HashMap;
import java.util.Map;
/** /**
* *
@ -64,18 +61,18 @@ public class ExternalVideoAssetForm
@Override @Override
protected void initForm(final PageState state, protected void initForm(final PageState state,
final Optional<ExternalVideoAsset> selectedAsset) { final Map<String, Object> data) {
super.initForm(state, selectedAsset);
if (selectedAsset.isPresent()) { super.initForm(state, data);
final ExternalVideoAsset externalVideoAsset
= (ExternalVideoAsset) selectedAsset
.get();
final LegalMetadata legalMetadata = externalVideoAsset final Long selectedAssetId = getSelectedAssetId(state);
.getLegalMetadata(); if (selectedAssetId != null) {
if (legalMetadata != null) {
assetSearchWidget.setValue(state, legalMetadata.getObjectId()); if (data.containsKey(
ExternalVideoAssetFormController.LEGAL_METADATA_ID)) {
assetSearchWidget.setValue(
state,
data.get(ExternalVideoAssetFormController.LEGAL_METADATA_ID));
} }
} }
} }
@ -85,61 +82,19 @@ public class ExternalVideoAssetForm
return ExternalVideoAsset.class; return ExternalVideoAsset.class;
} }
// @Override
// protected Asset createAsset(final FormSectionEvent event)
// throws FormProcessException {
//
// Objects.requireNonNull(event);
//
// final PageState state = event.getPageState();
//
// final ExternalVideoAsset externalVideoAsset = new ExternalVideoAsset();
//
// updateData((Bookmark) externalVideoAsset, state);
// updateData(externalVideoAsset, state);
//
// return externalVideoAsset;
// }
protected void updateData(final ExternalVideoAsset externalVideoAsset,
final PageState state) {
final Long legalMetadataId = (Long) assetSearchWidget.getValue(state);
if (legalMetadataId != null) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final AssetRepository assetRepo = cdiUtil.findBean(
AssetRepository.class);
final LegalMetadata legalMetadata = (LegalMetadata) assetRepo
.findById(legalMetadataId)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No LegalMetadata asset with ID %d in the database.",
legalMetadataId)));
externalVideoAsset.setLegalMetadata(legalMetadata);
}
}
@Override @Override
protected void updateAsset(final Asset asset, protected Map<String, Object> collectData(final FormSectionEvent event)
final FormSectionEvent event) { throws FormProcessException {
Objects.requireNonNull(asset);
Objects.requireNonNull(event);
final Map<String, Object> data = new HashMap<>();
final PageState state = event.getPageState(); final PageState state = event.getPageState();
if (!(asset instanceof ExternalVideoAsset)) { if (assetSearchWidget.getValue(state) != null) {
throw new IllegalArgumentException(String.format( data.put(ExternalAudioAssetFormController.LEGAL_METADATA_ID,
"Provided asset is not an instance of class (or sub class of) " assetSearchWidget.getValue(state));
+ "'%s' but is an instance of class '%s'",
ExternalVideoAsset.class.getName(),
asset.getClass().getName()));
} }
final ExternalVideoAsset externalVideoAsset = (ExternalVideoAsset) asset; return data;
updateData((Bookmark) externalVideoAsset, state);
updateData(externalVideoAsset, state);
} }
} }

View File

@ -0,0 +1,88 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.cms.ui.assets.IsControllerForAssetType;
import org.librecms.assets.ExternalVideoAsset;
import org.librecms.assets.LegalMetadata;
import org.librecms.contentsection.AssetRepository;
import java.util.Locale;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@IsControllerForAssetType(ExternalVideoAsset.class)
public class ExternalVideoAssetFormController
extends AbstractBookmarkFormController<ExternalVideoAsset> {
protected static final String LEGAL_METADATA_ID = "legalMetadataId";
@Inject
private AssetRepository assetRepository;
@Override
public ExternalVideoAsset createAsset() {
return new ExternalVideoAsset();
}
@Transactional(Transactional.TxType.REQUIRED)
@Override
protected Map<String, Object> getAssetData(final ExternalVideoAsset asset,
final Locale selectedLocale) {
final Map<String, Object> data = super.getAssetData(asset,
selectedLocale);
if (asset.getLegalMetadata() != null) {
data.put(LEGAL_METADATA_ID, asset.getLegalMetadata().getObjectId());
}
return data;
}
@Override
public void updateAssetProperties(final ExternalVideoAsset asset,
final Locale selectedLocale,
final Map<String, Object> data) {
super.updateAssetProperties(asset, selectedLocale, data);
if (data.containsKey(LEGAL_METADATA_ID)) {
final long legalMetadataId = (long) data.get(LEGAL_METADATA_ID);
final LegalMetadata legalMetadata = assetRepository
.findById(legalMetadataId, LegalMetadata.class)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No LegalMetadata with ID %d found.", legalMetadataId)));
asset.setLegalMetadata(legalMetadata);
}
}
}

View File

@ -18,9 +18,8 @@
*/ */
package com.arsdigita.cms.ui.assets.forms; package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.bebop.PageState;
import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.cms.ui.assets.AssetPane;
import org.librecms.assets.BinaryAsset;
import org.librecms.assets.FileAsset; import org.librecms.assets.FileAsset;
/** /**
@ -38,11 +37,4 @@ public class FileAssetForm extends AbstractBinaryAssetForm<FileAsset> {
return FileAsset.class; return FileAsset.class;
} }
// @Override
// protected BinaryAsset createBinaryAsset(final PageState state) {
// return new FileAsset();
// }
} }

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.cms.ui.assets.IsControllerForAssetType;
import org.librecms.assets.FileAsset;
import javax.enterprise.context.RequestScoped;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@IsControllerForAssetType(FileAsset.class)
public class FileAssetFormController extends AbstractBinaryAssetFormController<FileAsset> {
@Override
public FileAsset createAsset() {
return new FileAsset();
}
}

View File

@ -29,13 +29,12 @@ import com.arsdigita.globalization.GlobalizedMessage;
import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.CmsConstants; import org.librecms.CmsConstants;
import org.librecms.assets.BinaryAsset;
import org.librecms.assets.Image; import org.librecms.assets.Image;
import org.librecms.assets.LegalMetadata; import org.librecms.assets.LegalMetadata;
import org.librecms.contentsection.Asset; import org.librecms.contentsection.Asset;
import org.librecms.contentsection.AssetRepository; import org.librecms.contentsection.AssetRepository;
import java.util.Optional; import java.util.Map;
/** /**
* *
@ -83,24 +82,27 @@ public class ImageForm extends AbstractBinaryAssetForm<Image> {
@Override @Override
protected void initForm(final PageState state, protected void initForm(final PageState state,
final Optional<Image> selectedAsset) { final Map<String, Object> data) {
super.initForm(state, selectedAsset); super.initForm(state, data);
if (selectedAsset.isPresent()) { if (getSelectedAssetId(state) != null) {
final Image image = selectedAsset.get(); if (data.containsKey(ImageFormController.WIDTH)) {
final long widthValue = (long) data
.get(ImageFormController.WIDTH);
width.setValue(state, Long.toString(widthValue));
}
if (data.containsKey(ImageFormController.HEIGHT)) {
final long heightValue = (long) data
.get(ImageFormController.HEIGHT);
height.setValue(state, Long.toString(heightValue));
}
width.setValue(state, if (data.containsKey(ImageFormController.LEGAL_METADATA_ID)) {
Long.toString(image.getWidth())); assetSearchWidget
height.setValue(state, .setValue(state,
Long.toString(image.getHeight())); data.get(ImageFormController.LEGAL_METADATA_ID));
final LegalMetadata legalMetadata = image
.getLegalMetadata();
if (legalMetadata == null) {
assetSearchWidget.setValue(state, null);
} else {
assetSearchWidget.setValue(state, legalMetadata.getObjectId());
} }
} }
} }
@ -110,75 +112,22 @@ public class ImageForm extends AbstractBinaryAssetForm<Image> {
return Image.class; return Image.class;
} }
// @Override
// protected Asset createAsset(final FormSectionEvent event)
// throws FormProcessException {
//
// final Image image = (Image) super.createAsset(event);
//
// final PageState state = event.getPageState();
//
// image.setDisplayName(getTitleValue(state)
// .toLowerCase()
// .replace(" ", "-"));
//
// if (height.getValue(state) != null) {
// image.setHeight(Long.parseLong((String) height.getValue(state)));
// }
// if (width.getValue(state) != null) {
// image.setWidth(Long.parseLong((String) width.getValue(state)));
// }
// updateData(image, state);
//
// return image;
// }
@Override @Override
protected void updateAsset(final Asset asset, protected Map<String, Object> collectData(final FormSectionEvent event)
final FormSectionEvent event)
throws FormProcessException { throws FormProcessException {
super.updateAsset(asset, event); final Map<String, Object> data = super.collectData(event);
final PageState state = event.getPageState(); final PageState state = event.getPageState();
final Image image = (Image) asset; data.put(ImageFormController.WIDTH, width.getValue(state));
data.put(ImageFormController.HEIGHT, height.getValue(state));
if (height.getValue(state) == null if (assetSearchWidget.getValue(state) != null) {
|| ((String) height.getValue(state)).trim().isEmpty()) { data.put(ImageFormController.LEGAL_METADATA_ID,
image.setHeight(-1); assetSearchWidget.getValue(state));
} else {
image.setHeight(Long.parseLong((String) height.getValue(state)));
}
if (width.getValue(state) == null
|| ((String) width.getValue(state)).trim().isEmpty()) {
image.setWidth(-1);
} else {
image.setWidth(Long.parseLong((String) width.getValue(state)));
} }
updateData(image, state); return data;
} }
protected void updateData(final Image image,
final PageState state) {
final Long legalMetadataId = (Long) assetSearchWidget.getValue(state);
if (legalMetadataId != null) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final AssetRepository assetRepo = cdiUtil.findBean(
AssetRepository.class);
final LegalMetadata legalMetadata = (LegalMetadata) assetRepo
.findById(legalMetadataId)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No LegalMetadata asset with ID %d in the database.",
legalMetadataId)));
image.setLegalMetadata(legalMetadata);
}
}
// @Override
// protected BinaryAsset createBinaryAsset(final PageState state) {
// return new Image();
// }
} }

View File

@ -0,0 +1,100 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.cms.ui.assets.IsControllerForAssetType;
import org.librecms.assets.Image;
import org.librecms.assets.LegalMetadata;
import org.librecms.contentsection.AssetRepository;
import java.util.Locale;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@IsControllerForAssetType(Image.class)
public class ImageFormController
extends AbstractBinaryAssetFormController<Image> {
protected static final String LEGAL_METADATA_ID = "legalMetadataId";
protected static final String HEIGHT = "height";
protected static final String WIDTH = "width";
@Inject
private AssetRepository assetRepository;
@Override
public Image createAsset() {
return new Image();
}
@Override
protected Map<String, Object> getAssetData(final Image asset,
final Locale selectedLocale) {
final Map<String, Object> data = super.getAssetData(asset,
selectedLocale);
data.put(WIDTH, asset.getWidth());
data.put(HEIGHT, asset.getHeight());
if (asset.getLegalMetadata() != null) {
data.put(LEGAL_METADATA_ID,
asset.getLegalMetadata().getObjectId());
}
return data;
}
@Override
public void updateAssetProperties(final Image asset,
final Locale selectedLocale,
final Map<String, Object> data) {
super.updateAssetProperties(asset, selectedLocale, data);
if (data.containsKey(WIDTH)) {
asset.setWidth((long) data.get(WIDTH));
}
if (data.containsKey(HEIGHT)) {
asset.setHeight((long) data.get(HEIGHT));
}
if (data.containsKey(LEGAL_METADATA_ID)) {
final long legalMetadataId = (long) data.get(LEGAL_METADATA_ID);
final LegalMetadata legalMetadata = assetRepository
.findById(legalMetadataId, LegalMetadata.class)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No LegalMetadata with ID %d found.", legalMetadataId)));
asset.setLegalMetadata(legalMetadata);
}
}
}

View File

@ -32,6 +32,8 @@ import org.librecms.CmsConstants;
import org.librecms.assets.LegalMetadata; import org.librecms.assets.LegalMetadata;
import org.librecms.contentsection.Asset; import org.librecms.contentsection.Asset;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -84,47 +86,36 @@ public class LegalMetadataForm extends AbstractAssetForm<LegalMetadata> {
@Override @Override
protected void initForm(final PageState state, protected void initForm(final PageState state,
final Optional<LegalMetadata> selectedAsset) { final Map<String, Object> data) {
if (selectedAsset.isPresent()) { if (getSelectedAssetId(state) != null) {
if (!(selectedAsset.get() instanceof LegalMetadata)) { rightsHolder.setValue(
throw new IllegalArgumentException(String.format( state,
"The provided asset must be an instanceof of class '%s' or " data.get(LegalMetadataFormController.RIGHTS_HOLDER));
+ "an subclass but is an instanceof of class '%s'.",
LegalMetadata.class.getName(),
selectedAsset.get().getClass().getName()));
}
final LegalMetadata legalMetadata = selectedAsset.get();
rightsHolder.setValue(state, legalMetadata.getRightsHolder());
rights.setValue(state, rights.setValue(state,
legalMetadata data.get(LegalMetadataFormController.RIGHTS));
.getRights() publisher.setValue(state,
.getValue(getSelectedLocale(state))); data.get(LegalMetadataFormController.PUBLISHER));
publisher.setValue(state, legalMetadata.getPublisher()); creator.setValue(state,
creator.setValue(state, legalMetadata.getCreator()); data.get(LegalMetadataFormController.CREATOR));
} }
} }
@Override @Override
protected void showLocale(final PageState state) { protected void showLocale(final PageState state) {
final Optional<LegalMetadata> selectedAsset = getSelectedAsset(state); final Long selectedAssetId = getSelectedAssetId(state);
if (selectedAsset.isPresent()) { if (selectedAssetId != null) {
if (!(getSelectedAsset(state).get() instanceof LegalMetadata)) {
throw new IllegalArgumentException(
"Selected asset is not a legal metadata");
}
final LegalMetadata legalMetadata = selectedAsset.get(); final Map<String, Object> data = getController()
.getAssetData(selectedAssetId,
LegalMetadata.class,
getSelectedLocale(state));
rights.setValue(state, rights.setValue(state,
legalMetadata data.get(LegalMetadataFormController.RIGHTS));
.getRights()
.getValue(getSelectedLocale(state)));
} }
} }
@ -133,51 +124,21 @@ public class LegalMetadataForm extends AbstractAssetForm<LegalMetadata> {
return LegalMetadata.class; return LegalMetadata.class;
} }
// @Override
// protected Asset createAsset(final FormSectionEvent event)
// throws FormProcessException {
//
// Objects.requireNonNull(event);
//
// final PageState state = event.getPageState();
//
// final LegalMetadata legalMetadata = new LegalMetadata();
//
// legalMetadata.setRightsHolder((String) rightsHolder.getValue(state));
// legalMetadata.getRights().addValue(getSelectedLocale(state),
// (String) rights.getValue(state));
//
// legalMetadata.setPublisher((String) publisher.getValue(state));
// legalMetadata.setCreator((String) creator.getValue(state));
//
// return legalMetadata;
// }
@Override @Override
protected void updateAsset(final Asset asset, protected Map<String, Object> collectData(final FormSectionEvent event)
final FormSectionEvent event)
throws FormProcessException { throws FormProcessException {
Objects.requireNonNull(asset);
Objects.requireNonNull(event);
final PageState state = event.getPageState(); final PageState state = event.getPageState();
final Map<String, Object> data = new HashMap<>();
if (!(asset instanceof LegalMetadata)) { data.put(LegalMetadataFormController.CREATOR, creator.getValue(state));
throw new IllegalArgumentException(String.format( data.put(LegalMetadataFormController.PUBLISHER,
"Provided asset is not an instance of '%s' (or a sub class) " publisher.getValue(state));
+ "but is an instance of class '%s'.", data.put(LegalMetadataFormController.RIGHTS,
LegalMetadata.class.getName(), rights.getValue(state));
asset.getClass().getName())); data.put(LegalMetadataFormController.RIGHTS_HOLDER,
rightsHolder.getValue(state));
return data;
} }
final LegalMetadata legalMetadata = (LegalMetadata) asset;
legalMetadata.setRightsHolder((String) rightsHolder.getValue(state));
legalMetadata.getRights().addValue(getSelectedLocale(state),
(String) rights.getValue(state));
legalMetadata.setPublisher((String) publisher.getValue(state));
legalMetadata.setCreator((String) creator.getValue(state));
}
} }

View File

@ -0,0 +1,99 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.cms.ui.assets.AbstractAssetFormController;
import com.arsdigita.cms.ui.assets.IsControllerForAssetType;
import org.librecms.assets.LegalMetadata;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@IsControllerForAssetType(LegalMetadata.class)
public class LegalMetadataFormController
extends AbstractAssetFormController<LegalMetadata> {
protected static final String CONTRIBUTORS = "contributors";
protected static final String CREATOR = "creator";
protected static final String PUBLISHER = "publisher";
protected static final String RIGHTS = "rights";
protected static final String RIGHTS_HOLDER = "rightsHolder";
@Override
protected Map<String, Object> getAssetData(final LegalMetadata asset,
final Locale selectedLocale) {
final Map<String, Object> data = new HashMap<>();
data.put(RIGHTS_HOLDER, asset.getRightsHolder());
data.put(RIGHTS, asset.getRights().getValue(selectedLocale));
data.put(PUBLISHER, asset.getPublisher());
data.put(CREATOR, asset.getCreator());
data.put(CONTRIBUTORS, asset.getContributors());
return data;
}
@Override
public void updateAssetProperties(final LegalMetadata asset,
final Locale selectedLocale,
final Map<String, Object> data) {
if (data.containsKey(RIGHTS_HOLDER)) {
asset.setRightsHolder((String) data.get(RIGHTS_HOLDER));
}
if (data.containsKey(RIGHTS)) {
asset.getRights().addValue(selectedLocale,
(String) data.get(RIGHTS));
}
if (data.containsKey(PUBLISHER)) {
asset.setPublisher((String) data.get(PUBLISHER));
}
if (data.containsKey(CREATOR)) {
asset.setCreator((String) data.get(CREATOR));
}
if (data.containsKey(CONTRIBUTORS)) {
@SuppressWarnings("unchecked")
final List<String> contributors = (List<String>) data
.get(CONTRIBUTORS);
asset.setContributors(contributors);
}
}
@Override
public LegalMetadata createAsset() {
return new LegalMetadata();
}
}

View File

@ -28,10 +28,9 @@ import com.arsdigita.globalization.GlobalizedMessage;
import org.librecms.CmsConstants; import org.librecms.CmsConstants;
import org.librecms.assets.Organization; import org.librecms.assets.Organization;
import org.librecms.contentsection.Asset;
import java.util.Objects; import java.util.HashMap;
import java.util.Optional; import java.util.Map;
/** /**
* *
@ -47,17 +46,16 @@ public class OrganizationForm extends AbstractContactableEntityForm<Organization
} }
@Override @Override
public void init(final FormSectionEvent event) throws FormProcessException { public void initForm(final PageState state,
final Map<String, Object> data) {
super.init(event); super.initForm(state, data);
final PageState state = event.getPageState(); if (getSelectedAssetId(state) != null) {
final Optional<Organization> selected = getSelectedAsset(state); organizationName.setValue(
state,
if (selected.isPresent()) { data.get(OrganizationFormController.ORGANIZATION_NAME));
organizationName.setValue(state, selected.get().getName());
} }
} }
@ -84,26 +82,16 @@ public class OrganizationForm extends AbstractContactableEntityForm<Organization
} }
@Override @Override
protected void updateAsset(final Asset asset, protected Map<String, Object> collectData(final FormSectionEvent event)
final FormSectionEvent event)
throws FormProcessException { throws FormProcessException {
Objects.requireNonNull(asset);
Objects.requireNonNull(event);
final PageState state = event.getPageState(); final PageState state = event.getPageState();
if (!(asset instanceof Organization)) { final Map<String, Object> data = new HashMap<>();
throw new IllegalArgumentException(String.format( data.put(OrganizationFormController.ORGANIZATION_NAME,
"The provided asset is not an instance of \"%s\" " organizationName.getValue(state));
+ "or a subclass,but of %s.",
Organization.class.getName(),
asset.getClass().getName()));
}
final Organization organization = (Organization) asset; return data;
organization.setName((String) organizationName.getValue(state));
} }
} }

View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.cms.ui.assets.IsControllerForAssetType;
import org.librecms.assets.Organization;
import java.util.Locale;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@IsControllerForAssetType(Organization.class)
public class OrganizationFormController
extends AbstractContactableEntityFormController<Organization> {
protected static final String ORGANIZATION_NAME = "organizationName";
@Override
protected Map<String, Object> getAssetData(final Organization asset,
final Locale selectedLocale) {
final Map<String, Object> data = super.getAssetData(asset,
selectedLocale);
data.put(ORGANIZATION_NAME, asset.getName());
return data;
}
@Override
public void updateAssetProperties(final Organization asset,
final Locale selectedLocale,
final Map<String, Object> data) {
super.updateAssetProperties(asset, selectedLocale, data);
if (data.containsKey(ORGANIZATION_NAME)) {
final String organizationName = (String) data.get(ORGANIZATION_NAME);
asset.setName(organizationName);
}
}
@Override
public Organization createAsset() {
return new Organization();
}
}

View File

@ -27,12 +27,11 @@ import com.arsdigita.cms.ui.assets.AbstractAssetForm;
import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.cms.ui.assets.AssetPane;
import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.globalization.GlobalizedMessage;
import java.util.Objects;
import java.util.Optional;
import org.librecms.CmsConstants; import org.librecms.CmsConstants;
import org.librecms.assets.SideNote; import org.librecms.assets.SideNote;
import org.librecms.contentsection.Asset;
import java.util.HashMap;
import java.util.Map;
/** /**
* *
@ -57,23 +56,11 @@ public class SideNoteForm extends AbstractAssetForm<SideNote> {
@Override @Override
protected void initForm(final PageState state, protected void initForm(final PageState state,
final Optional<SideNote> selectedAsset) { final Map<String, Object> data) {
if (selectedAsset.isPresent()) { if (getSelectedAssetId(state) != null) {
if (!(selectedAsset.get() instanceof SideNote)) {
throw new IllegalArgumentException(String.format(
"The provided asset must be an instance of '%s' or"
+ "an subclass but is an instance of '%s'",
SideNote.class.getName(),
selectedAsset.get().getClass().getName()));
}
final SideNote sideNote = selectedAsset.get(); text.setValue(state, data.get(SideNoteFormController.TEXT));
text.setValue(state,
sideNote
.getText()
.getValue(getSelectedLocale(state)));
} }
} }
@ -81,23 +68,15 @@ public class SideNoteForm extends AbstractAssetForm<SideNote> {
@Override @Override
protected void showLocale(final PageState state) { protected void showLocale(final PageState state) {
final Optional<SideNote> selectedAsset = getSelectedAsset(state); if (getSelectedAssetId(state) != null) {
if (selectedAsset.isPresent()) { final Long selectedAssetId = getSelectedAssetId(state);
if (!(selectedAsset.get() instanceof SideNote)) { final Map<String, Object> data = getController()
throw new IllegalArgumentException(String.format( .getAssetData(selectedAssetId,
"The provided asset must be an instance of '%s' or" SideNote.class,
+ "an subclass but is an instance of '%s'", getSelectedLocale(state));
SideNote.class.getName(),
selectedAsset.get().getClass().getName()));
}
final SideNote sideNote =selectedAsset.get(); text.setValue(state, data.get(SideNoteFormController.TEXT));
text.setValue(state,
sideNote
.getText()
.getValue(getSelectedLocale(state)));
} }
} }
@ -106,50 +85,16 @@ public class SideNoteForm extends AbstractAssetForm<SideNote> {
return SideNote.class; return SideNote.class;
} }
// @Override
// protected Asset createAsset(final FormSectionEvent event) throws
// FormProcessException {
//
// Objects.requireNonNull(event);
//
// final PageState state = event.getPageState();
//
// final SideNote sideNote = new SideNote();
//
// sideNote
// .getText()
// .addValue(getSelectedLocale(state),
// (String) text.getValue(state));
//
// return sideNote;
// }
@Override @Override
protected void updateAsset(final Asset asset, protected Map<String, Object> collectData(final FormSectionEvent event)
final FormSectionEvent event)
throws FormProcessException { throws FormProcessException {
Objects.requireNonNull(asset); final Map<String, Object> data = new HashMap<>();
Objects.requireNonNull(event);
final PageState state = event.getPageState(); final PageState state = event.getPageState();
if (!(asset instanceof SideNote)) { data.put(SideNoteFormController.TEXT, text.getValue(state));
throw new IllegalArgumentException(String.format(
"The provided asset must be an instance of '%s' or"
+ "an subclass but is an instance of '%s'",
SideNote.class.getName(),
asset.getClass().getName()));
}
final SideNote sideNote = (SideNote) asset; return data;
sideNote
.getText()
.addValue(getSelectedLocale(state),
(String) text.getValue(state));
} }
} }

View File

@ -0,0 +1,72 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.cms.ui.assets.AbstractAssetFormController;
import com.arsdigita.cms.ui.assets.IsControllerForAssetType;
import org.librecms.assets.SideNote;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@IsControllerForAssetType(SideNote.class)
public class SideNoteFormController
extends AbstractAssetFormController<SideNote> {
protected static final String TEXT = "text";
@Override
protected Map<String, Object> getAssetData(final SideNote asset,
final Locale selectedLocale) {
final Map<String, Object> data = new HashMap<>();
data.put(TEXT, asset.getText().getValue(selectedLocale));
return data;
}
@Override
public void updateAssetProperties(final SideNote asset,
final Locale selectedLocale,
final Map<String, Object> data) {
if (data.containsKey(TEXT)) {
final String value = (String) data.get(TEXT);
asset.getText().addValue(selectedLocale, value);
}
}
@Override
public SideNote createAsset() {
return new SideNote();
}
}

View File

@ -34,7 +34,7 @@ import org.librecms.assets.LegalMetadata;
import org.librecms.contentsection.Asset; import org.librecms.contentsection.Asset;
import org.librecms.contentsection.AssetRepository; import org.librecms.contentsection.AssetRepository;
import java.util.Optional; import java.util.Map;
/** /**
* *
@ -57,7 +57,8 @@ public class VideoForm extends AbstractBinaryAssetForm<VideoAsset> {
width = new TextField("width-text"); width = new TextField("width-text");
height = new TextField("height-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( add(new Label(new GlobalizedMessage(
"cms.ui.assets.video.width.label", "cms.ui.assets.video.width.label",
@ -80,22 +81,25 @@ public class VideoForm extends AbstractBinaryAssetForm<VideoAsset> {
@Override @Override
protected void initForm(final PageState state, protected void initForm(final PageState state,
final Optional<VideoAsset> selectedAsset) { final Map<String, Object> data) {
super.initForm(state, selectedAsset); super.initForm(state, data);
if (selectedAsset.isPresent()) { if (getSelectedAssetId(state) != null) {
final VideoAsset video = selectedAsset.get(); final long widthValue = (long) data.get(VideoFormController.WIDTH);
final long heightValue = (long) data
.get(VideoFormController.HEIGHT);
width.setValue(state, Long.toString(widthValue));
height.setValue(state, Long.toString(heightValue));
if (data.containsKey(VideoFormController.LEGAL_METADATA_ID)) {
assetSearchWidget.setValue(
state,
data.containsKey(VideoFormController.LEGAL_METADATA_ID));
width.setValue(state,
Long.toString(video.getWidth()));
height.setValue(state,
Long.toString(video.getHeight()));
final LegalMetadata legalMetadata = video
.getLegalMetadata();
if (legalMetadata != null) {
assetSearchWidget.setValue(state, legalMetadata.getObjectId());
} }
} }
} }
@ -105,60 +109,25 @@ public class VideoForm extends AbstractBinaryAssetForm<VideoAsset> {
return VideoAsset.class; return VideoAsset.class;
} }
// @Override
// protected Asset createAsset(final FormSectionEvent event)
// throws FormProcessException {
//
// final VideoAsset video = (VideoAsset) super.createAsset(event);
//
// final PageState state = event.getPageState();
//
// video.setHeight(Long.parseLong((String) height.getValue(state)));
// video.setWidth(Long.parseLong((String) width.getValue(state)));
// updateData(video, state);
//
// return video;
// }
@Override @Override
protected void updateAsset(final Asset asset, protected Map<String, Object> collectData(final FormSectionEvent event)
final FormSectionEvent event)
throws FormProcessException { throws FormProcessException {
super.updateAsset(asset, event);
final PageState state = event.getPageState(); final PageState state = event.getPageState();
final VideoAsset video = (VideoAsset) asset; final Map<String, Object> data = super.collectData(event);
video.setHeight(Long.parseLong((String) height.getValue(state))); data.put(VideoFormController.WIDTH, width.getValue(state));
video.setWidth(Long.parseLong((String) width.getValue(state))); data.put(VideoFormController.HEIGHT, height.getValue(state));
if (assetSearchWidget.getValue(state) != null) {
data.put(VideoFormController.LEGAL_METADATA_ID,
assetSearchWidget.getValue(state));
updateData(video, state);
} }
protected void updateData(final VideoAsset video, return data;
final PageState state) {
final Long legalMetadataId = (Long) assetSearchWidget.getValue(state);
if (legalMetadataId != null) {
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final AssetRepository assetRepo = cdiUtil.findBean(
AssetRepository.class);
final LegalMetadata legalMetadata = (LegalMetadata) assetRepo
.findById(legalMetadataId)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No LegalMetadata asset with ID %d in the database.",
legalMetadataId)));
video.setLegalMetadata(legalMetadata);
}
} }
// @Override
// protected BinaryAsset createBinaryAsset(final PageState state) {
// return new VideoAsset();
// }
} }

View File

@ -0,0 +1,104 @@
/*
* Copyright (C) 2019 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.arsdigita.cms.ui.assets.forms;
import com.arsdigita.cms.ui.assets.IsControllerForAssetType;
import com.arsdigita.cms.ui.assets.forms.AbstractBinaryAssetFormController;
import org.librecms.contentsection.AssetRepository;
import java.util.Locale;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import org.librecms.assets.LegalMetadata;
import org.librecms.assets.VideoAsset;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@IsControllerForAssetType(VideoAsset.class)
public class VideoFormController
extends AbstractBinaryAssetFormController<VideoAsset> {
protected static final String LEGAL_METADATA_ID = "legalMetadataId";
protected static final String HEIGHT = "height";
protected static final String WIDTH = "width";
@Inject
private AssetRepository assetRepository;
@Override
protected Map<String, Object> getAssetData(final VideoAsset asset,
final Locale selectedLocale) {
final Map<String, Object> data = super.getAssetData(asset,
selectedLocale);
data.put(WIDTH, asset.getWidth());
data.put(HEIGHT, asset.getHeight());
if (asset.getLegalMetadata() != null) {
final long legalMetadataId = asset.getLegalMetadata().getObjectId();
data.put(LEGAL_METADATA_ID, legalMetadataId);
}
return data;
}
@Override
public void updateAssetProperties(final VideoAsset asset,
final Locale selectedLocale,
final Map<String, Object> data) {
super.updateAssetProperties(asset, selectedLocale, data);
if (data.containsKey(WIDTH)) {
final long width = (long) data.get(WIDTH);
asset.setWidth(width);
}
if (data.containsKey(HEIGHT)) {
final long height = (long) data.get(HEIGHT);
asset.setHeight(height);
}
if (data.containsKey(LEGAL_METADATA_ID)) {
final LegalMetadata legalMetadata = assetRepository
.findById((long) data.get(LEGAL_METADATA_ID),
LegalMetadata.class)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No LegalMetadata with ID %d found",
data.get(LEGAL_METADATA_ID)
)));
asset.setLegalMetadata(legalMetadata);
}
}
@Override
public VideoAsset createAsset() {
return new VideoAsset();
}
}