This is an automated email from the ASF dual-hosted git repository. danhaywood pushed a commit to branch CAUSEWAY-3597 in repository https://gitbox.apache.org/repos/asf/causeway.git
commit a6953532c3694b782b7e4b3c245810e3309eedb1 Author: danhaywood <[email protected]> AuthorDate: Thu Sep 28 16:54:34 2023 +0100 CAUSEWAY-3597: adds additional mixin to download all columnOrder txt files for a domain class as a single zip --- .../inspect/Object_downloadColumnOrderTxtFile.java | 10 +-- ...> Object_downloadColumnOrderTxtFilesAsZip.java} | 84 +++++++++++----------- .../jdo/metamodel/menu/JdoMetamodelMenu.java | 5 ++ ...ayout.xml => FixtureResult.layout.fallback.xml} | 5 +- 4 files changed, 56 insertions(+), 48 deletions(-) diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFile.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFile.java index b3cbb303a4..d3be456772 100644 --- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFile.java +++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFile.java @@ -85,7 +85,7 @@ public class Object_downloadColumnOrderTxtFile { String fileName = String.format("%s.columnOrder.txt", parentSpec.getShortIdentifier()); String fileContents = buf.toString(); - return newClon(fileName, fileContents); + return newClob(fileName, fileContents); } private Clob parentedCollectionTxtFile(String collectionId) { @@ -107,7 +107,7 @@ public class Object_downloadColumnOrderTxtFile { String fileName = String.format("%s#%s.columnOrder.txt", parentSpec.getShortIdentifier(), collectionId); String fileContents = buf.toString(); - return newClon(fileName, fileContents); + return newClob(fileName, fileContents); } @MemberSupport public List<String> choices0Act() { @@ -117,11 +117,7 @@ public class Object_downloadColumnOrderTxtFile { .collect(Collectors.toList()); } - @MemberSupport public String disableAct() { - return choices0Act().isEmpty() ? "No collections" : null; - } - - private static Clob newClon(String fileName, String fileContents) { + private static Clob newClob(String fileName, String fileContents) { return Clob.of(fileName, NamedWithMimeType.CommonMimeType.TXT, fileContents); } diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFile.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFilesAsZip.java similarity index 62% copy from core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFile.java copy to core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFilesAsZip.java index b3cbb303a4..fcbc39b520 100644 --- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFile.java +++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/inspect/Object_downloadColumnOrderTxtFilesAsZip.java @@ -18,13 +18,15 @@ */ package org.apache.causeway.core.metamodel.inspect; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - import javax.inject.Inject; -import org.springframework.lang.Nullable; +import org.apache.causeway.applib.value.Blob; + +import org.apache.causeway.commons.io.ZipUtils; + +import org.apache.causeway.core.metamodel.spec.ObjectSpecification; + +import org.apache.causeway.core.metamodel.spec.feature.OneToManyAssociation; import org.apache.causeway.applib.annotation.Action; import org.apache.causeway.applib.annotation.ActionLayout; @@ -45,37 +47,49 @@ import lombok.val; @Action( commandPublishing = Publishing.DISABLED, - domainEvent = Object_downloadColumnOrderTxtFile.ActionDomainEvent.class, + domainEvent = Object_downloadColumnOrderTxtFilesAsZip.ActionDomainEvent.class, executionPublishing = Publishing.DISABLED, restrictTo = RestrictTo.PROTOTYPING, semantics = SemanticsOf.IDEMPOTENT // to avoid caching ) @ActionLayout( - describedAs = "Downloads a .columnOrder.txt file for either this object or one of its collections", + cssClassFa = "fa-download", + named = "Download .columnOrder.txt files (ZIP)", + describedAs = "Downloads all the .columnOrder.txt files for this object and its collections, as a zip file", fieldSetId = LayoutConstants.FieldSetId.METADATA, position = ActionLayout.Position.PANEL_DROPDOWN, - sequence = "700.2.2" + sequence = "700.2.3" ) @RequiredArgsConstructor -public class Object_downloadColumnOrderTxtFile { +public class Object_downloadColumnOrderTxtFilesAsZip { private final Object domainObject; // mixee public static class ActionDomainEvent - extends org.apache.causeway.applib.CausewayModuleApplib.ActionDomainEvent<Object_downloadColumnOrderTxtFile> {} + extends org.apache.causeway.applib.CausewayModuleApplib.ActionDomainEvent<Object_downloadColumnOrderTxtFilesAsZip> {} @Inject SpecificationLoader specificationLoader; - @MemberSupport public Clob act( - @Nullable String collectionId - ) { + @MemberSupport public Blob act(final String fileName) { + + val zipBuilder = ZipUtils.zipEntryBuilder(); - return collectionId == null - ? standaloneCollectionTxtFile() - : parentedCollectionTxtFile(collectionId); + addStandaloneEntry(zipBuilder); + addCollectionEntries(zipBuilder); + + final byte[] zipBytes = zipBuilder.toBytes(); + return Blob.of(fileName, NamedWithMimeType.CommonMimeType.ZIP, zipBytes); + } + + public String default0Act() { + val parentSpec = specificationLoader.loadSpecification(domainObject.getClass()); + return String.format("%s.columnOrder.zip", parentSpec.getShortIdentifier()); } - private Clob standaloneCollectionTxtFile() { + + // HELPERS + + private void addStandaloneEntry(ZipUtils.EntryBuilder zipBuilder) { val parentSpec = specificationLoader.loadSpecification(domainObject.getClass()); val buf = new StringBuilder(); @@ -83,19 +97,21 @@ public class Object_downloadColumnOrderTxtFile { .map(ObjectFeature::getId) .forEach(assocId -> buf.append(assocId).append("\n")); - String fileName = String.format("%s.columnOrder.txt", parentSpec.getShortIdentifier()); - String fileContents = buf.toString(); - return newClon(fileName, fileContents); + val fileName = String.format("%s.columnOrder.txt", parentSpec.getShortIdentifier()); + val fileContents = buf.toString(); + + zipBuilder.addAsUtf8(fileName, fileContents); } - private Clob parentedCollectionTxtFile(String collectionId) { + private void addCollectionEntries(ZipUtils.EntryBuilder zipBuilder) { val parentSpec = specificationLoader.loadSpecification(domainObject.getClass()); + parentSpec.streamCollections(MixedIn.INCLUDED) + .forEach(collection -> addCollection(collection, parentSpec, zipBuilder)); + } + + private void addCollection(OneToManyAssociation collection, ObjectSpecification parentSpec, ZipUtils.EntryBuilder zipBuilder) { val buf = new StringBuilder(); - val collection = parentSpec.streamCollections(MixedIn.INCLUDED) - .filter(x -> Objects.equals(x.getId(), collectionId)) - .findFirst() - .orElseThrow(); // shouldn't happen because of disableAct guard. val collectionIdentifier = collection.getFeatureIdentifier(); val elementType = collection.getElementType(); @@ -105,24 +121,12 @@ public class Object_downloadColumnOrderTxtFile { .map(ObjectFeature::getId) .forEach(assocId -> buf.append(assocId).append("\n")); - String fileName = String.format("%s#%s.columnOrder.txt", parentSpec.getShortIdentifier(), collectionId); - String fileContents = buf.toString(); - return newClon(fileName, fileContents); - } + val fileName = String.format("%s#%s.columnOrder.txt", parentSpec.getShortIdentifier(), collection.getId()); + val fileContents = buf.toString(); - @MemberSupport public List<String> choices0Act() { - val objectSpec = specificationLoader.loadSpecification(domainObject.getClass()); - return objectSpec.streamCollections(MixedIn.INCLUDED) - .map(ObjectFeature::getId) - .collect(Collectors.toList()); + zipBuilder.addAsUtf8(fileName, fileContents); } - @MemberSupport public String disableAct() { - return choices0Act().isEmpty() ? "No collections" : null; - } - private static Clob newClon(String fileName, String fileContents) { - return Clob.of(fileName, NamedWithMimeType.CommonMimeType.TXT, fileContents); - } } diff --git a/persistence/jdo/metamodel/src/main/java/org/apache/causeway/persistence/jdo/metamodel/menu/JdoMetamodelMenu.java b/persistence/jdo/metamodel/src/main/java/org/apache/causeway/persistence/jdo/metamodel/menu/JdoMetamodelMenu.java index af4ac394ad..38554cc8f0 100644 --- a/persistence/jdo/metamodel/src/main/java/org/apache/causeway/persistence/jdo/metamodel/menu/JdoMetamodelMenu.java +++ b/persistence/jdo/metamodel/src/main/java/org/apache/causeway/persistence/jdo/metamodel/menu/JdoMetamodelMenu.java @@ -36,6 +36,9 @@ import org.apache.causeway.applib.annotation.SemanticsOf; import org.apache.causeway.applib.value.Blob; import org.apache.causeway.applib.value.NamedWithMimeType.CommonMimeType; import org.apache.causeway.commons.io.ZipUtils; +import org.apache.causeway.core.metamodel.spec.feature.MixedIn; +import org.apache.causeway.core.metamodel.spec.feature.ObjectAssociation; +import org.apache.causeway.core.metamodel.spec.feature.ObjectFeature; import org.apache.causeway.persistence.jdo.applib.services.JdoSupportService; import org.apache.causeway.persistence.jdo.metamodel.CausewayModulePersistenceJdoMetamodel; import org.apache.causeway.persistence.jdo.provider.entities.JdoFacetContext; @@ -43,6 +46,8 @@ import org.apache.causeway.persistence.jdo.provider.entities.JdoFacetContext; import lombok.RequiredArgsConstructor; import lombok.val; +import java.util.Objects; + @Named(CausewayModulePersistenceJdoMetamodel.NAMESPACE + ".JdoMetamodelMenu") @DomainService() @DomainServiceLayout( diff --git a/testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureResult.layout.xml b/testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureResult.layout.fallback.xml similarity index 80% rename from testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureResult.layout.xml rename to testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureResult.layout.fallback.xml index 4cafa1772e..ab86f8fef0 100644 --- a/testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureResult.layout.xml +++ b/testing/fixtures/applib/src/main/java/org/apache/causeway/testing/fixtures/applib/fixturescripts/FixtureResult.layout.fallback.xml @@ -17,7 +17,10 @@ specific language governing permissions and limitations under the License. --> -<bs:grid xsi:schemaLocation="https://causeway.apache.org/applib/layout/component https://causeway.apache.org/applib/layout/component/component.xsd https://causeway.apache.org/applib/layout/grid/bootstrap3 https://causeway.apache.org/applib/layout/grid/bootstrap3/bootstrap3.xsd" xmlns:bs="https://causeway.apache.org/applib/layout/grid/bootstrap3" xmlns:cpt="https://causeway.apache.org/applib/layout/component" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> +<bs:grid + xsi:schemaLocation="https://causeway.apache.org/applib/layout/component https://causeway.apache.org/applib/layout/component/component.xsd https://causeway.apache.org/applib/layout/grid/bootstrap3 https://causeway.apache.org/applib/layout/grid/bootstrap3/bootstrap3.xsd" + xmlns:bs="https://causeway.apache.org/applib/layout/grid/bootstrap3" + xmlns:cpt="https://causeway.apache.org/applib/layout/component" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <bs:row> <bs:col span="12" unreferencedActions="true"> <cpt:domainObject/>
