diff --git a/sci-publications/src/main/java/org/scientificcms/publications/ui/assets/CollectedVolumeAssetEditStep.java b/sci-publications/src/main/java/org/scientificcms/publications/ui/assets/CollectedVolumeAssetEditStep.java index 4b5fcae..989cf84 100644 --- a/sci-publications/src/main/java/org/scientificcms/publications/ui/assets/CollectedVolumeAssetEditStep.java +++ b/sci-publications/src/main/java/org/scientificcms/publications/ui/assets/CollectedVolumeAssetEditStep.java @@ -26,7 +26,6 @@ import org.scientificcms.publications.PublicationManager; import org.scientificcms.publications.PublicationRepository; import org.scientificcms.publications.Publisher; import org.scientificcms.publications.PublisherManager; -import org.scientificcms.publications.PublisherRepository; import org.scientificcms.publications.assets.CollectedVolumeAsset; import org.scientificcms.publications.assets.PublisherAsset; import org.scientificcms.publications.assets.SeriesAsset; @@ -743,7 +742,12 @@ public class CollectedVolumeAssetEditStep extends AbstractMvcAssetEditStep { @Path("/publisher/@remove") @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) - public String removePublisher() { + public String removePublisher( + @PathParam(MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME) + final String assetPath + ) { try { init(); } catch (ContentSectionNotFoundException ex) { diff --git a/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationPropertiesStep.java b/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationPropertiesStep.java index 2f76f74..24a61f8 100644 --- a/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationPropertiesStep.java +++ b/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationPropertiesStep.java @@ -1,8 +1,44 @@ package org.scientificcms.publications.ui.contenttypes; +import org.libreccm.api.Identifier; +import org.libreccm.api.IdentifierParser; +import org.libreccm.l10n.GlobalizationHelper; +import org.libreccm.security.AuthorizationRequired; +import org.librecms.assets.Person; +import org.librecms.assets.PersonRepository; +import org.librecms.contentsection.AssetRepository; +import org.librecms.contentsection.ContentItemRepository; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; +import org.librecms.ui.contentsections.ItemPermissionChecker; +import org.librecms.ui.contentsections.assets.AssetNotFoundException; import org.librecms.ui.contentsections.documents.AbstractMvcAuthoringStep; +import org.librecms.ui.contentsections.documents.DocumentNotFoundException; +import org.librecms.ui.contentsections.documents.DocumentUi; +import org.librecms.ui.contentsections.documents.MvcAuthoringSteps; +import org.scientificcms.publications.Authorship; import org.scientificcms.publications.Publication; +import org.scientificcms.publications.PublicationManager; +import org.scientificcms.publications.PublicationRepository; +import org.scientificcms.publications.assets.PublicationAsset; import org.scientificcms.publications.contenttypes.PublicationItem; +import org.scientificcms.publications.ui.AuthorsTableRow; +import org.scientificcms.publications.ui.SciPublicationsUiMessageBundle; + +import java.util.List; +import java.util.Locale; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.inject.Inject; +import javax.mvc.Models; +import javax.transaction.Transactional; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; /** * @@ -12,5 +48,670 @@ import org.scientificcms.publications.contenttypes.PublicationItem; */ public abstract class AbstractPublicationPropertiesStep, P extends Publication> extends AbstractMvcAuthoringStep { - + + @Inject + private AssetRepository assetRepo; + + @Inject + private ContentItemRepository itemRepo; + + @Inject + private DocumentUi documentUi; + + @Inject + private GlobalizationHelper globalizationHelper; + + @Inject + private IdentifierParser identifierParser; + + @Inject + private ItemPermissionChecker itemPermissionChecker; + + @Inject + private Models models; + + @Inject + private PersonRepository personRepo; + + @Inject + private PublicationManager publicationManager; + + @Inject + private PublicationPropertiesStepModel propertiesStepModel; + + @Inject + private PublicationRepository publicationRepo; + + @Inject + private SciPublicationsUiMessageBundle messageBundle; + + @SuppressWarnings("unchecked") + protected T getPublicationItem() { + return (T) getDocument(); + } + + protected P getPublication() { + return getPublicationItem().getPublication(); + } + + @Override + @Transactional(Transactional.TxType.REQUIRED) + protected void init() throws ContentSectionNotFoundException, + DocumentNotFoundException { + super.init(); + + if (!(getDocument() instanceof PublicationItem)) { + throw new DocumentNotFoundException( + documentUi.showDocumentNotFound( + getContentSection(), + getDocumentPath() + ) + ); + } + + final T item = getPublicationItem(); + final P publication = getPublication(); + + propertiesStepModel.setName(item.getDisplayName()); + + final Set titleLocales = publication + .getTitle() + .getAvailableLocales(); + + propertiesStepModel.setTitleValues( + publication + .getTitle() + .getValues() + .entrySet() + .stream() + .collect( + Collectors.toMap( + entry -> entry.getKey().toString(), + entry -> entry.getValue() + ) + ) + ); + + propertiesStepModel.setUnusedTitleLocales( + globalizationHelper + .getAvailableLocales() + .stream() + .filter(locale -> !titleLocales.contains(locale)) + .map(Locale::toString) + .collect(Collectors.toList()) + ); + + propertiesStepModel.setShortDecriptionValues( + publication + .getShortDescription() + .getValues() + .entrySet() + .stream() + .collect( + Collectors.toMap( + entry -> entry.getKey().toString(), + entry -> entry.getValue() + ) + ) + ); + + final Set shortDescriptionLocales = publication + .getShortDescription() + .getAvailableLocales(); + + propertiesStepModel.setUnusedShortDescriptionLocales( + globalizationHelper + .getAvailableLocales() + .stream() + .filter(locale -> !shortDescriptionLocales.contains(locale)) + .map(Locale::toString) + .collect(Collectors.toList()) + ); + + propertiesStepModel.setAuthors( + publication + .getAuthorships() + .stream() + .map(this::buildAuthorsTableRow) + .collect(Collectors.toList()) + ); + } + + private AuthorsTableRow buildAuthorsTableRow(final Authorship authorship) { + final AuthorsTableRow row = new AuthorsTableRow(); + row.setAuthorName( + String.format( + "%s, %s", + authorship.getAuthor().getPersonName().getSurname(), + authorship.getAuthor().getPersonName().getGivenName() + ) + ); + row.setAuthorshipId(authorship.getAuthorshipId()); + row.setAuthorshipUuid(authorship.getUuid()); + row.setEditor(authorship.isEditor()); + + return row; + } + + @GET + @Path("/") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String showStep( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + return getStepTemplatePath(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + messageBundle.getMessage("publication.edit.denied") + ); + } + } + + /** + * Updates the name of the current publication item. + * + * @param sectionIdentifier + * @param documentPath + * @param name + * + * @return A redirect to this authoring step. + */ + @POST + @Path("/name") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String updateName( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @FormParam("name") @DefaultValue("") final String name + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + if (name.isEmpty() || name.matches("\\s*")) { + models.put("nameMissing", true); + + return showStep(sectionIdentifier, documentPath); + } + + getDocument().setDisplayName(name); + itemRepo.save(getDocument()); + + updateDocumentPath(); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + messageBundle.getMessage("publication.edit.denied") + ); + } + } + + /** + * Updates a localized title of a publication. + * + * @param sectionIdentifier + * @param documentPath + * @param localeParam The locale to update. + * @param value The updated title value. + * + * @return A redirect to this authoring step. + */ + @POST + @Path("/title/@add") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String addTitle( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @FormParam("locale") final String localeParam, + @FormParam("value") final String value + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Locale locale = new Locale(localeParam); + getDocument().getTitle().putValue(locale, value); + itemRepo.save(getDocument()); + + getPublication().getTitle().putValue(locale, value); + publicationRepo.save(getPublication()); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + /** + * Updates a localized title of the publication. + * + * @param sectionIdentifier + * @param documentPath + * @param localeParam The locale to update. + * @param value The updated title value. + * + * @return A redirect to this authoring step. + */ + @POST + @Path("/title/@edit/{locale}") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String editTitle( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @PathParam("locale") final String localeParam, + @FormParam("value") final String value + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Locale locale = new Locale(localeParam); + getDocument().getTitle().putValue(locale, value); + itemRepo.save(getDocument()); + + getPublication().getTitle().putValue(locale, value); + publicationRepo.save(getPublication()); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + /** + * Removes a localized title of the publication. + * + * @param sectionIdentifier + * @param documentPath + * @param localeParam The locale to remove. + * + * @return A redirect to this authoring step. + */ + @POST + @Path("/title/@remove/{locale}") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String removeTitle( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @PathParam("locale") final String localeParam + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Locale locale = new Locale(localeParam); + getDocument().getTitle().removeValue(locale); + itemRepo.save(getDocument()); + + getPublication().getTitle().removeValue(locale); + publicationRepo.save(getPublication()); + + return buildRedirectPathForStep(); + + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + @POST + @Path("/shortdescription/add") + @Transactional(Transactional.TxType.REQUIRED) + public String addShortDescription( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @FormParam("locale") final String localeParam, + @FormParam("value") final String value + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Locale locale = new Locale(localeParam); + getPublication().getShortDescription().putValue(locale, value); + publicationRepo.save(getPublication()); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + @POST + @Path("/shortdescription/add/{locale}") + @Transactional(Transactional.TxType.REQUIRED) + public String editShortDescription( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @PathParam("locale") final String localeParam, + @FormParam("value") final String value + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Locale locale = new Locale(localeParam); + getPublication().getShortDescription().putValue(locale, value); + publicationRepo.save(getPublication()); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + @POST + @Path("/shortdescription/remove/{locale}") + @Transactional(Transactional.TxType.REQUIRED) + public String removeShortDescription( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @PathParam("locale") final String localeParam + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Locale locale = new Locale(localeParam); + getPublication().getShortDescription().removeValue(locale); + publicationRepo.save(getPublication()); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + public String addAuthor( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @FormParam("authorIdentifier") + final String authorIdentifier, + @FormParam("editor") + final String editorParam + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Identifier identifier = identifierParser.parseIdentifier( + authorIdentifier + ); + final Optional authorResult; + switch (identifier.getType()) { + case ID: + authorResult = personRepo.findById( + Long.parseLong(identifier.getIdentifier()) + ); + break; + case UUID: + authorResult = assetRepo.findByUuidAndType( + identifier.getIdentifier(), + Person.class + ); + break; + default: { + final List result = personRepo.findBySurname( + identifier.getIdentifier() + ); + if (result.isEmpty()) { + authorResult = Optional.empty(); + } else { + authorResult = Optional.of(result.get(0)); + } + break; + } + } + + if (authorResult.isEmpty()) { + return showAuthorNotFound( + sectionIdentifier, + documentPath, + authorIdentifier + ); + } + + final Person author = authorResult.get(); + final boolean editor = "true".equalsIgnoreCase(editorParam) + || "on".equalsIgnoreCase(editorParam); + publicationManager.addAuthor(author, getPublication(), editor); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + @POST + @Path("/authors/{authorshipUuid}") + @AuthorizationRequired + @Transactional + public String editAuthorship( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @PathParam("authorshipUuid") + final String authorshipUuid, + @FormParam("editor") + final String editorParam + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Optional authorshipResult = getPublication() + .getAuthorships() + .stream() + .filter(current -> current.getUuid().equals(authorshipUuid)) + .findAny(); + + if (authorshipResult.isEmpty()) { + return showAuthorshipNotFound( + sectionIdentifier, + documentPath, + authorshipUuid + ); + } + + final Authorship authorship = authorshipResult.get(); + authorship.setEditor( + "true".equalsIgnoreCase(editorParam) + || "on".equalsIgnoreCase(editorParam) + ); + + publicationRepo.save(getPublication()); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + @POST + @Path("/authors/{authorshipUuid}/remove") + @AuthorizationRequired + @Transactional + public String removeAuthorship( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @PathParam("authorshipUuid") + final String authorshipUuid + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Optional authorshipResult = getPublication() + .getAuthorships() + .stream() + .filter(current -> current.getUuid().equals(authorshipUuid)) + .findAny(); + + if (authorshipResult.isEmpty()) { + return showAuthorshipNotFound( + sectionIdentifier, + documentPath, + authorshipUuid + ); + } + + final Authorship authorship = authorshipResult.get(); + publicationManager.removeAuthor( + authorship.getAuthor(), + getPublication() + ); + + return buildRedirectPathForStep(); + + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + protected abstract String getStepTemplatePath(); + + private String showAuthorNotFound( + final String sectionIdentifier, + final String documentPath, + final String authorIdentifier + ) { + models.put("authorNotFound", authorIdentifier); + return showStep(sectionIdentifier, documentPath); + } + + private String showAuthorshipNotFound( + final String sectionIdentifier, + final String documentPath, + final String authorshipIdentifier + ) { + models.put("authorshipNotFound", authorshipIdentifier); + return showStep(sectionIdentifier, documentPath); + } + } diff --git a/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationWithPublisherItemCreateStep.java b/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationWithPublisherItemCreateStep.java index 90ec239..4c300f5 100644 --- a/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationWithPublisherItemCreateStep.java +++ b/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationWithPublisherItemCreateStep.java @@ -9,9 +9,7 @@ import org.scientificcms.publications.contenttypes.PublicationWithPublisherItem; * @param Subclass of {@link PublicationWithPublisherItem} * @param

Subclass of {@link PublicationWithPublisher} */ -public abstract class AbstractPublicationWithPublisherItemCreateStep, P extends PublicationWithPublisher> -extends AbstractPublicationItemCreateStep { - - - +public abstract class AbstractPublicationWithPublisherItemCreateStep, P extends PublicationWithPublisher> + extends AbstractPublicationItemCreateStep { + } diff --git a/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationWithPublisherPropertiesStep.java b/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationWithPublisherPropertiesStep.java index 998f70c..4a7b2ca 100644 --- a/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationWithPublisherPropertiesStep.java +++ b/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/AbstractPublicationWithPublisherPropertiesStep.java @@ -1,8 +1,29 @@ package org.scientificcms.publications.ui.contenttypes; +import org.libreccm.api.Identifier; +import org.libreccm.api.IdentifierParser; +import org.librecms.contentsection.Asset; +import org.librecms.contentsection.AssetRepository; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; +import org.librecms.ui.contentsections.ItemPermissionChecker; import org.librecms.ui.contentsections.documents.AbstractMvcAuthoringStep; -import org.scientificcms.publications.Publication; +import org.librecms.ui.contentsections.documents.DocumentNotFoundException; +import org.librecms.ui.contentsections.documents.DocumentUi; +import org.librecms.ui.contentsections.documents.MvcAuthoringSteps; +import org.scientificcms.publications.PublicationWithPublisher; +import org.scientificcms.publications.Publisher; +import org.scientificcms.publications.PublisherManager; +import org.scientificcms.publications.assets.PublisherAsset; import org.scientificcms.publications.contenttypes.PublicationItem; +import org.scientificcms.publications.contenttypes.PublicationWithPublisherItem; + +import java.util.Optional; + +import javax.inject.Inject; +import javax.mvc.Models; +import javax.transaction.Transactional; +import javax.ws.rs.FormParam; +import javax.ws.rs.PathParam; /** * @@ -10,7 +31,168 @@ import org.scientificcms.publications.contenttypes.PublicationItem; * @param Subtype of {@link PublicationWithPublisherItem} * @param

Subtype of {@link PublicationWithPublisher}. */ -public abstract class AbstractPublicationWithPublisherPropertiesStep, P extends Publication> - extends AbstractMvcAuthoringStep { +public abstract class AbstractPublicationWithPublisherPropertiesStep, P extends PublicationWithPublisher> + extends AbstractPublicationPropertiesStep { + + @Inject + private AssetRepository assetRepo; + + @Inject + private DocumentUi documentUi; + + @Inject + private IdentifierParser identifierParser; + + @Inject + private ItemPermissionChecker itemPermissionChecker; + + @Inject + private Models models; + + @Inject + private PublisherManager publisherManager; + + @Inject + private PublicationWithPublisherPropertiesStepModel propertiesStepModel; + + + @Override + @Transactional(Transactional.TxType.REQUIRED) + protected void init() throws ContentSectionNotFoundException, + DocumentNotFoundException { + super.init(); + + if (!(getDocument() instanceof PublicationItem)) { + throw new DocumentNotFoundException( + documentUi.showDocumentNotFound( + getContentSection(), + getDocumentPath() + ) + ); + } + + final P publication = getPublication(); + final Publisher publisher = publication.getPublisher(); + + propertiesStepModel.setPublisherUuid(publication.getUuid()); + propertiesStepModel.setPublisherName(publisher.getName()); + propertiesStepModel.setPublisherPlace(publisher.getPlace()); + } + + public String setPublisher( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath, + @FormParam("publisherIdentifier") + final String publisherIdentifier + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Identifier identifier = identifierParser.parseIdentifier( + publisherIdentifier + ); + final Optional assetResult; + switch (identifier.getType()) { + case ID: + assetResult = assetRepo.findById( + Long.parseLong(identifier.getIdentifier()), + PublisherAsset.class + ); + break; + case UUID: + assetResult = assetRepo.findByUuidAndType( + identifier.getIdentifier(), + PublisherAsset.class + ); + break; + default: { + final Optional result = assetRepo.findByPath( + identifier.getIdentifier() + ); + if (result == null || result.isEmpty()) { + assetResult = Optional.empty(); + } else if (!(result.get() instanceof PublisherAsset)) { + assetResult = Optional.empty(); + } else { + assetResult = Optional.of((PublisherAsset) result.get()); + } + break; + } + } + if (!assetResult.isPresent()) { + return showPublisherNotFound( + sectionIdentifier, + documentPath, + publisherIdentifier + ); + } + + final Publisher publisher = assetResult.get().getPublisher(); + + publisherManager.addPublicationToPublisher( + getPublication(), + publisher + ); + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + public String removePublisher( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPath + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (DocumentNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (itemPermissionChecker.canEditItem(getDocument())) { + final Publisher publisher = getPublication() + .getPublisher(); + if (publisher != null) { + publisherManager.removePublicationFromPublisher( + getPublication(), + publisher + ); + } + + return buildRedirectPathForStep(); + } else { + return documentUi.showAccessDenied( + getContentSection(), + getDocument(), + getLabel() + ); + } + } + + private String showPublisherNotFound( + final String sectionIdentifier, + final String documentPath, + final String publisherIdentifier + ) { + models.put("publisherNotFound", publisherIdentifier); + return super.showStep(sectionIdentifier, documentPath); + } } diff --git a/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/MonographPropertiesStep.java b/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/MonographPropertiesStep.java index f2b981d..2a5b22c 100644 --- a/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/MonographPropertiesStep.java +++ b/sci-publications/src/main/java/org/scientificcms/publications/ui/contenttypes/MonographPropertiesStep.java @@ -1,6 +1,8 @@ package org.scientificcms.publications.ui.contenttypes; import org.librecms.contenttypes.Event; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; +import org.librecms.ui.contentsections.documents.DocumentNotFoundException; import org.librecms.ui.contentsections.documents.MvcAuthoringStepDef; import org.librecms.ui.contentsections.documents.MvcAuthoringSteps; import org.librecms.ui.contenttypes.event.EventStepsConstants; @@ -9,6 +11,7 @@ import org.scientificcms.publications.contenttypes.MonographItem; import javax.enterprise.context.RequestScoped; import javax.mvc.Controller; +import javax.transaction.Transactional; import javax.ws.rs.Path; /** @@ -32,6 +35,12 @@ public class MonographPropertiesStep extends AbstractPublicationWithPublisherPro return MonographPropertiesStep.class; } - + @Override + @Transactional(Transactional.TxType.REQUIRED) + protected void init() throws ContentSectionNotFoundException, + DocumentNotFoundException { + super.init(); + + } }