You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2017/12/07 15:01:00 UTC
[isis] 04/18: ISIS-1784: adds TeardownFixtureAbstract and
TeardownFixtureAbstract2,
as ready-to-use fixture scripts to delete all instances of an entity
This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
commit 2f077b902354818edd6e3a1c96bc4e1901b1948e
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Mon Dec 4 08:41:35 2017 +0000
ISIS-1784: adds TeardownFixtureAbstract and TeardownFixtureAbstract2, as ready-to-use fixture scripts to delete all instances of an entity
---
.../teardown/TeardownFixtureAbstract.java | 68 +++++++++
.../teardown/TeardownFixtureAbstract2.java | 152 +++++++++++++++++++++
2 files changed, 220 insertions(+)
diff --git a/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/teardown/TeardownFixtureAbstract.java b/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/teardown/TeardownFixtureAbstract.java
new file mode 100644
index 0000000..319d3a8
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/teardown/TeardownFixtureAbstract.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.applib.fixturescripts.teardown;
+
+import javax.inject.Inject;
+import javax.jdo.metadata.TypeMetadata;
+
+import com.google.common.base.Strings;
+
+import org.apache.isis.applib.fixturescripts.FixtureScript;
+import org.apache.isis.applib.services.jdosupport.IsisJdoSupport;
+
+public abstract class TeardownFixtureAbstract extends FixtureScript {
+
+ protected void deleteFrom(final Class cls) {
+ preDeleteFrom(cls);
+ final TypeMetadata metadata = isisJdoSupport.getJdoPersistenceManager().getPersistenceManagerFactory()
+ .getMetadata(cls.getName());
+ if(metadata == null) {
+ // fall-back
+ deleteFrom(cls.getSimpleName());
+ } else {
+ final String schema = metadata.getSchema();
+ String table = metadata.getTable();
+ if(Strings.isNullOrEmpty(table)) {
+ table = cls.getSimpleName();
+ }
+ if(Strings.isNullOrEmpty(schema)) {
+ deleteFrom(table);
+ } else {
+ deleteFrom(schema, table);
+ }
+ }
+ postDeleteFrom(cls);
+ }
+
+ protected Integer deleteFrom(final String schema, final String table) {
+ return isisJdoSupport.executeUpdate(String.format("DELETE FROM \"%s\".\"%s\"", schema, table));
+ }
+
+ protected void deleteFrom(final String table) {
+ isisJdoSupport.executeUpdate(String.format("DELETE FROM \"%s\"", table));
+ }
+
+ protected void preDeleteFrom(final Class cls) {}
+
+ protected void postDeleteFrom(final Class cls) {}
+
+ @Inject
+ private IsisJdoSupport isisJdoSupport;
+
+}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/teardown/TeardownFixtureAbstract2.java b/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/teardown/TeardownFixtureAbstract2.java
new file mode 100644
index 0000000..c40dc67
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/fixturescripts/teardown/TeardownFixtureAbstract2.java
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.applib.fixturescripts.teardown;
+
+import javax.inject.Inject;
+import javax.jdo.PersistenceManagerFactory;
+import javax.jdo.annotations.DiscriminatorStrategy;
+import javax.jdo.annotations.InheritanceStrategy;
+import javax.jdo.metadata.DiscriminatorMetadata;
+import javax.jdo.metadata.InheritanceMetadata;
+import javax.jdo.metadata.TypeMetadata;
+
+import com.google.common.base.Strings;
+
+import org.apache.isis.applib.services.jdosupport.IsisJdoSupport;
+
+public abstract class TeardownFixtureAbstract2 extends TeardownFixtureAbstract {
+
+
+ @Override
+ protected void deleteFrom(Class cls) {
+ final String column = discriminatorColumnOf(cls);
+ final String value = discriminatorValueOf(cls);
+ if(column == null || value == null) {
+ superDeleteFrom(cls);
+ return;
+ }
+ final String schema = schemaOf(cls);
+ final String table = tableOf(cls);
+
+ this.preDeleteFrom(cls);
+ if (Strings.isNullOrEmpty(schema)) {
+ this.deleteFromWhere(table, column, value);
+ } else {
+ this.deleteFromWhere(schema, table, column, value);
+ }
+ this.postDeleteFrom(cls);
+ }
+
+ protected String schemaOf(final Class cls) {
+ TypeMetadata metadata = getPersistenceManagerFactory().getMetadata(cls.getName());
+ if(metadata == null) {
+ return null;
+ }
+ final InheritanceMetadata inheritanceMetadata = metadata.getInheritanceMetadata();
+ if(inheritanceMetadata != null && inheritanceMetadata.getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE) {
+ return schemaOf(cls.getSuperclass());
+ }
+ return metadata.getSchema();
+ }
+
+ protected String tableOf(final Class cls) {
+ final TypeMetadata metadata = getPersistenceManagerFactory().getMetadata(cls.getName());
+ if(metadata == null) {
+ return cls.getSimpleName();
+ }
+ final InheritanceMetadata inheritanceMetadata = metadata.getInheritanceMetadata();
+ if(inheritanceMetadata != null && inheritanceMetadata.getStrategy() == InheritanceStrategy.SUPERCLASS_TABLE) {
+ return tableOf(cls.getSuperclass());
+ }
+ final String table = metadata.getTable();
+ return !Strings.isNullOrEmpty(table) ? table : cls.getSimpleName();
+ }
+
+ protected String discriminatorValueOf(final Class cls) {
+ TypeMetadata metadata = getPersistenceManagerFactory().getMetadata(cls.getName());
+ if(metadata == null) {
+ return null;
+ }
+ final InheritanceMetadata inheritanceMetadata = metadata.getInheritanceMetadata();
+ if(inheritanceMetadata == null ||
+ inheritanceMetadata.getStrategy() != InheritanceStrategy.SUPERCLASS_TABLE) {
+ return null;
+ }
+ final DiscriminatorMetadata discriminatorMetadata = inheritanceMetadata.getDiscriminatorMetadata();
+ if(discriminatorMetadata == null ||
+ discriminatorMetadata.getStrategy() != DiscriminatorStrategy.VALUE_MAP) {
+ return null;
+ }
+ return discriminatorMetadata.getValue();
+ }
+
+ protected String discriminatorColumnOf(final Class cls) {
+ final String discriminator = doDiscriminatorOf(cls);
+ return discriminator != null ? discriminator : "discriminator";
+ }
+
+ private String doDiscriminatorOf(final Class cls) {
+ final TypeMetadata metadata = getPersistenceManagerFactory().getMetadata(cls.getName());
+ if(metadata == null) {
+ return null;
+ }
+ final InheritanceMetadata inheritanceMetadata = metadata.getInheritanceMetadata();
+ if(inheritanceMetadata == null ||
+ inheritanceMetadata.getStrategy() != InheritanceStrategy.SUPERCLASS_TABLE) {
+ return null;
+ }
+ final DiscriminatorMetadata discriminatorMetadata = inheritanceMetadata.getDiscriminatorMetadata();
+ if(discriminatorMetadata == null ||
+ discriminatorMetadata.getStrategy() != DiscriminatorStrategy.VALUE_MAP) {
+ return null;
+ }
+ return discriminatorMetadata.getColumn();
+ }
+
+ private PersistenceManagerFactory getPersistenceManagerFactory() {
+ return isisJdoSupport.getJdoPersistenceManager().getPersistenceManagerFactory();
+ }
+
+ private void superDeleteFrom(final Class cls) {
+ super.deleteFrom(cls);
+ }
+
+ protected Integer deleteFromWhere(String schema, String table, String column, String value) {
+ final String sql = String.format(
+ "DELETE FROM \"%s\".\"%s\" WHERE \"%s\"='%s'",
+ schema, table, column, value);
+ return this.isisJdoSupport.executeUpdate(sql);
+ }
+
+ protected void deleteFromWhere(String table, String column, String value) {
+ final String sql = String.format(
+ "DELETE FROM \"%s\" WHERE \"%s\"='%s'",
+ table, column, value);
+ this.isisJdoSupport.executeUpdate(sql);
+ }
+
+ protected void preDeleteFrom(Class cls) {
+ }
+
+ protected void postDeleteFrom(Class cls) {
+ }
+
+ @Inject
+ protected IsisJdoSupport isisJdoSupport;
+}
--
To stop receiving notification emails like this one, please contact
"commits@isis.apache.org" <co...@isis.apache.org>.