Infrastructure for models of Assets and some models for assets

pull/10/head
Jens Pelzetter 2021-11-04 19:38:56 +01:00
parent 2d5da3dc7f
commit a9b36bd775
12 changed files with 640 additions and 2 deletions

View File

@ -0,0 +1,66 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
/**
* A simpli
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public abstract class AbstractAssetModel {
private String uuid;
private String displayName;
private String title;
public String getUuid() {
return uuid;
}
public void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getTitle() {
return title;
}
public void setTitle(final String title) {
this.title = title;
}
/**
* Returns the type of the assets. In most cases implementations
* should return the fully qualified name of the asset class here.
*
* @return The type of the asset.
*/
public abstract String getType();
}

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.contentsection.Asset;
import javax.inject.Inject;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
* @param <T>
* @param <M>
*/
public abstract class AbstractAssetModelBuilder<T extends Asset, M extends AbstractAssetModel>
implements AssetModelBuilder<T, M> {
@Inject
private GlobalizationHelper globalizationHelper;
@Override
@SuppressWarnings("unchecked")
public M buildAssetModel(final Asset asset) {
final M model = buildModel();
model.setDisplayName(asset.getDisplayName());
model.setTitle(
globalizationHelper.getValueFromLocalizedString(asset.getTitle())
);
model.setUuid(asset.getUuid());
addProperties((T) asset, model);
return model;
}
protected abstract M buildModel();
protected void addProperties(final T asset, final M model) {
// Nothing in the default implementation.
}
}

View File

@ -53,6 +53,7 @@ public abstract class AbstractContentItemListItemModelBuilder<T extends ContentI
contentItem.getTitle()
)
);
model.setUuid(contentItem.getUuid());
addProperties((T) contentItem, model);

View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.librecms.contentsection.Asset;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public interface AssetModelBuilder<T extends Asset, M extends AbstractAssetModel> {
M buildAssetModel(Asset asset);
Class<T> buildsAssetModelFor();
}

View File

@ -0,0 +1,85 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.librecms.contentsection.Asset;
import java.util.Optional;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class AssetModelBuilders {
@Inject
private Instance<AssetModelBuilder<?, ?>> modelBuilders;
public Optional<AssetModelBuilder<?, ?>> getModelBuilderFor(
final Class<? extends Asset> assetType
) {
final Optional<AssetModelBuilder<?, ?>> exactMatch = findExactType(
assetType
);
if (exactMatch.isPresent()) {
return exactMatch;
} else {
return findForParentType(assetType);
}
}
private Optional<AssetModelBuilder<?, ?>> findExactType(
final Class<? extends Asset> assetType
) {
return modelBuilders
.stream()
.filter(
modelBuilder -> modelBuilder.buildsAssetModelFor().equals(
assetType
)
)
.findAny();
}
private Optional<AssetModelBuilder<?, ?>> findForParentType(
final Class<? extends Asset> assetType
) {
final Class<?> superClass = assetType.getSuperclass();
if (Asset.class.isAssignableFrom(superClass)) {
@SuppressWarnings("unchecked")
final Class<? extends Asset> superType
= (Class<? extends Asset>) superClass;
final Optional<AssetModelBuilder<?, ?>> builderForSuperType
= findExactType(superType);
if (builderForSuperType.isPresent()) {
return builderForSuperType;
} else {
return findForParentType(superType);
}
} else {
return Optional.empty();
}
}
}

View File

@ -53,8 +53,11 @@ public class ContentItemListItemModelBuilders {
) {
return modelBuilders
.stream()
.filter(modelBuilder -> modelBuilder.buildsListItemModelFor()
.equals(itemType))
.filter(
modelBuilder -> modelBuilder.buildsListItemModelFor().equals(
itemType
)
)
.findAny();
}

View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.librecms.assets.LegalMetadata;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class LegalMetadataModel extends AbstractAssetModel{
private String rightsHolder;
private String rights;
private String publisher;
private String creator;
private List<String> contributors;
@Override
public String getType() {
return LegalMetadata.class.getName();
}
public String getRightsHolder() {
return rightsHolder;
}
public void setRightsHolder(final String rightsHolder) {
this.rightsHolder = rightsHolder;
}
public String getRights() {
return rights;
}
public void setRights(final String rights) {
this.rights = rights;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(final String publisher) {
this.publisher = publisher;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public List<String> getContributors() {
return Collections.unmodifiableList(contributors);
}
public void setContributors(final List<String> contributors) {
this.contributors = new ArrayList<>(contributors);
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.assets.LegalMetadata;
import javax.inject.Inject;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class LegalMetadataModelBuilder
extends AbstractAssetModelBuilder<LegalMetadata, LegalMetadataModel> {
@Inject
private GlobalizationHelper globalizationHelper;
@Override
public Class<LegalMetadata> buildsAssetModelFor() {
return LegalMetadata.class;
}
@Override
protected LegalMetadataModel buildModel() {
return new LegalMetadataModel();
}
@Override
protected void addProperties(
final LegalMetadata legalMetadata, final LegalMetadataModel model
) {
super.addProperties(legalMetadata, model);
model.setContributors(legalMetadata.getContributors());
model.setCreator(legalMetadata.getCreator());
model.setPublisher(legalMetadata.getPublisher());
model.setRights(
globalizationHelper.getValueFromLocalizedString(
legalMetadata.getRights()
)
);
model.setRightsHolder(legalMetadata.getRightsHolder());
}
}

View File

@ -0,0 +1,85 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.librecms.assets.PostalAddress;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class PostalAddressModel extends AbstractAssetModel {
private String address;
private String postalCode;
private String city;
private String state;
private String isoCountryCode;
@Override
public String getType() {
return PostalAddress.class.getName();
}
public String getAddress() {
return address;
}
public void setAddress(final String address) {
this.address = address;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(final String postalCode) {
this.postalCode = postalCode;
}
public String getCity() {
return city;
}
public void setCity(final String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(final String state) {
this.state = state;
}
public String getIsoCountryCode() {
return isoCountryCode;
}
public void setIsoCountryCode(final String isoCountryCode) {
this.isoCountryCode = isoCountryCode;
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.librecms.assets.PostalAddress;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class PostalAddressModelBuilder
extends AbstractAssetModelBuilder<PostalAddress, PostalAddressModel> {
@Override
public Class<PostalAddress> buildsAssetModelFor() {
return PostalAddress.class;
}
@Override
protected PostalAddressModel buildModel() {
return new PostalAddressModel();
}
@Override
protected void addProperties(
final PostalAddress postalAddress, final PostalAddressModel model
) {
super.addProperties(postalAddress, model);
model.setAddress(postalAddress.getAddress());
model.setCity(postalAddress.getCity());
model.setIsoCountryCode(postalAddress.getIsoCountryCode());
model.setPostalCode(postalAddress.getPostalCode());
model.setState(postalAddress.getState());
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.librecms.assets.SideNote;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class SideNoteModel extends AbstractAssetModel {
private String text;
@Override
public String getType() {
return SideNote.class.getName();
}
public String getText() {
return text;
}
public void setText(final String text) {
this.text = text;
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (C) 2021 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 org.librecms.pages.models;
import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.assets.SideNote;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class SideNoteModelBuilder
extends AbstractAssetModelBuilder<SideNote, SideNoteModel> {
@Inject
private GlobalizationHelper globalizationHelper;
@Override
public Class<SideNote> buildsAssetModelFor() {
return SideNote.class;
}
@Override
protected SideNoteModel buildModel() {
return new SideNoteModel();
}
@Override
protected void addProperties(
final SideNote sideNote, final SideNoteModel model
) {
super.addProperties(sideNote, model);
model.setText(
globalizationHelper.getValueFromLocalizedString(sideNote.getText())
);
}
}