From 4d73b11d126fed907cab2bd844cbf2d059489f0f Mon Sep 17 00:00:00 2001 From: dhrn <14145706+dhrn@users.noreply.github.com> Date: Mon, 15 Feb 2021 16:59:23 +0530 Subject: [PATCH] show model in the aspect/type api (#285) --- .../rest/api/model/AbstractClass.java | 31 +++++ .../org/alfresco/rest/api/model/Aspect.java | 1 + .../org/alfresco/rest/api/model/Model.java | 105 +++++++++++++++++ .../org/alfresco/rest/api/model/Type.java | 1 + .../rest/api/tests/BaseModelApiTest.java | 106 ++++++++++++++++++ .../alfresco/rest/api/tests/TestAspects.java | 43 +------ .../alfresco/rest/api/tests/TestTypes.java | 40 +------ .../rest/api/tests/client/data/Aspect.java | 23 ++++ .../rest/api/tests/client/data/Type.java | 23 ++++ 9 files changed, 293 insertions(+), 80 deletions(-) create mode 100644 remote-api/src/main/java/org/alfresco/rest/api/model/Model.java create mode 100644 remote-api/src/test/java/org/alfresco/rest/api/tests/BaseModelApiTest.java diff --git a/remote-api/src/main/java/org/alfresco/rest/api/model/AbstractClass.java b/remote-api/src/main/java/org/alfresco/rest/api/model/AbstractClass.java index cac150a299..ff4baf8edb 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/model/AbstractClass.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/model/AbstractClass.java @@ -26,6 +26,9 @@ package org.alfresco.rest.api.model; +import org.alfresco.service.cmr.dictionary.ModelDefinition; +import org.alfresco.service.cmr.dictionary.NamespaceDefinition; +import org.alfresco.service.cmr.i18n.MessageLookup; import org.alfresco.service.namespace.QName; import java.util.ArrayList; @@ -38,6 +41,7 @@ public abstract class AbstractClass extends ClassDefinition implements Comparabl String title; String description; String parentId; + Model model; public String getId() { @@ -79,6 +83,16 @@ public abstract class AbstractClass extends ClassDefinition implements Comparabl this.parentId = parentId; } + public Model getModel() + { + return model; + } + + public void setModel(Model model) + { + this.model = model; + } + List setList(List sourceList) { if (sourceList == null) @@ -97,6 +111,23 @@ public abstract class AbstractClass extends ClassDefinition implements Comparabl return null; } + Model getModelInfo(org.alfresco.service.cmr.dictionary.ClassDefinition classDefinition, MessageLookup messageLookup) + { + final ModelDefinition modelDefinition = classDefinition.getModel(); + final String prefix = classDefinition.getName().toPrefixString().split(":")[0]; + + final NamespaceDefinition namespaceDefinition = modelDefinition.getNamespaces().stream() + .filter(definition -> definition.getPrefix().equals(prefix)) + .findFirst() + .get(); + + final String modelId = modelDefinition.getName().toPrefixString(); + final String author = modelDefinition.getAuthor(); + final String description = modelDefinition.getDescription(messageLookup); + + return new Model(modelId, author, description, namespaceDefinition.getUri(), namespaceDefinition.getPrefix()); + } + @Override public int hashCode() { diff --git a/remote-api/src/main/java/org/alfresco/rest/api/model/Aspect.java b/remote-api/src/main/java/org/alfresco/rest/api/model/Aspect.java index 6bf9cf3494..c7fabee629 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/model/Aspect.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/model/Aspect.java @@ -44,6 +44,7 @@ public class Aspect extends AbstractClass this.description = aspectDefinition.getDescription(messageLookup); this.parentId = getParentNameAsString(aspectDefinition.getParentName()); this.properties = setList(properties); + this.model = getModelInfo(aspectDefinition, messageLookup); } @Override diff --git a/remote-api/src/main/java/org/alfresco/rest/api/model/Model.java b/remote-api/src/main/java/org/alfresco/rest/api/model/Model.java new file mode 100644 index 0000000000..ae3cd744ae --- /dev/null +++ b/remote-api/src/main/java/org/alfresco/rest/api/model/Model.java @@ -0,0 +1,105 @@ +/* + * #%L + * Alfresco Remote API + * %% + * Copyright (C) 2005 - 2021 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco 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 3 of the License, or + * (at your option) any later version. + * + * Alfresco 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 Alfresco. If not, see . + * #L% + */ + +package org.alfresco.rest.api.model; + +public class Model implements Comparable +{ + private String id; + private String author; + private String description; + private String namespaceUri; + private String namespacePrefix; + + public Model() + { + } + + public Model(String name, String author, String description, String namespaceUri, String namespacePrefix) + { + this.id = name; + this.author = author; + this.description = description; + this.namespaceUri = namespaceUri; + this.namespacePrefix = namespacePrefix; + } + + public String getId() + { + return id; + } + + public void setId(String id) + { + this.id = id; + } + + public String getAuthor() + { + return author; + } + + public void setAuthor(String author) + { + this.author = author; + } + + public String getDescription() + { + return description; + } + + public void setDescription(String description) + { + this.description = description; + } + + public String getNamespaceUri() + { + return namespaceUri; + } + + public void setNamespaceUri(String namespaceUri) + { + this.namespaceUri = namespaceUri; + } + + public String getNamespacePrefix() + { + return namespacePrefix; + } + + public void setNamespacePrefix(String namespacePrefix) + { + this.namespacePrefix = namespacePrefix; + } + + @Override + public int compareTo(Model model) + { + return this.id.compareTo(model.getId()); + } +} diff --git a/remote-api/src/main/java/org/alfresco/rest/api/model/Type.java b/remote-api/src/main/java/org/alfresco/rest/api/model/Type.java index 3eb8fdded6..1e19a7e8da 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/model/Type.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/model/Type.java @@ -44,6 +44,7 @@ public class Type extends AbstractClass this.description = typeDefinition.getDescription(messageLookup); this.parentId = getParentNameAsString(typeDefinition.getParentName()); this.properties = setList(properties); + this.model = getModelInfo(typeDefinition, messageLookup); } @Override diff --git a/remote-api/src/test/java/org/alfresco/rest/api/tests/BaseModelApiTest.java b/remote-api/src/test/java/org/alfresco/rest/api/tests/BaseModelApiTest.java new file mode 100644 index 0000000000..c57aab0efe --- /dev/null +++ b/remote-api/src/test/java/org/alfresco/rest/api/tests/BaseModelApiTest.java @@ -0,0 +1,106 @@ +/* + * #%L + * Alfresco Remote API + * %% + * Copyright (C) 2005 - 2021 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco 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 3 of the License, or + * (at your option) any later version. + * + * Alfresco 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 Alfresco. If not, see . + * #L% + */ + +package org.alfresco.rest.api.tests; + +import org.alfresco.rest.api.model.Model; +import org.alfresco.rest.api.tests.client.PublicApiClient; +import org.junit.Before; + +import java.util.HashMap; +import java.util.Map; + +public class BaseModelApiTest extends AbstractBaseApiTest +{ + PublicApiClient.ListResponse aspects = null; + org.alfresco.rest.api.tests.client.data.Aspect aspect = null, childAspect = null, smartFilter = null, rescanAspect = null; + + + PublicApiClient.ListResponse types = null; + org.alfresco.rest.api.tests.client.data.Type type = null, whitePaperType = null, docType = null; + + + PublicApiClient.Paging paging = getPaging(0, 10); + Map otherParams = new HashMap<>(); + + @Before + public void setup() throws Exception + { + super.setup(); + + Model myCompanyModel = new Model(); + myCompanyModel.setAuthor("Administrator"); + myCompanyModel.setId("mycompany:model"); + myCompanyModel.setNamespaceUri("http://www.mycompany.com/model/finance/1.0"); + myCompanyModel.setNamespacePrefix("mycompany"); + + Model scanModel = new Model(); + scanModel.setAuthor("Administrator"); + scanModel.setId("test:scan"); + scanModel.setNamespaceUri("http://www.test.com/model/account/1.0"); + scanModel.setNamespacePrefix("test"); + + childAspect = new org.alfresco.rest.api.tests.client.data.Aspect(); + childAspect.setId("mycompany:childAspect"); + childAspect.setTitle("Child Aspect"); + childAspect.setDescription("Child Aspect Description"); + childAspect.setParentId("smf:smartFolder"); + childAspect.setModel(myCompanyModel); + + rescanAspect = new org.alfresco.rest.api.tests.client.data.Aspect(); + rescanAspect.setId("test:rescan"); + rescanAspect.setTitle("rescan"); + rescanAspect.setDescription("Doc that required to scan "); + rescanAspect.setModel(scanModel); + + smartFilter = new org.alfresco.rest.api.tests.client.data.Aspect(); + smartFilter.setId("test:smartFilter"); + smartFilter.setTitle("Smart filter"); + smartFilter.setDescription("Smart Filter"); + smartFilter.setParentId("cm:auditable"); + smartFilter.setModel(scanModel); + + whitePaperType = new org.alfresco.rest.api.tests.client.data.Type(); + whitePaperType.setId("mycompany:whitepaper"); + whitePaperType.setTitle("whitepaper"); + whitePaperType.setDescription("Whitepaper"); + whitePaperType.setParentId("mycompany:doc"); + whitePaperType.setModel(myCompanyModel); + + docType = new org.alfresco.rest.api.tests.client.data.Type(); + docType.setId("mycompany:doc"); + docType.setTitle("doc"); + docType.setDescription("Doc"); + docType.setParentId("cm:content"); + docType.setModel(myCompanyModel); + } + + @Override + public String getScope() + { + return "public"; + } +} diff --git a/remote-api/src/test/java/org/alfresco/rest/api/tests/TestAspects.java b/remote-api/src/test/java/org/alfresco/rest/api/tests/TestAspects.java index ab6ec1a841..e1be516873 100644 --- a/remote-api/src/test/java/org/alfresco/rest/api/tests/TestAspects.java +++ b/remote-api/src/test/java/org/alfresco/rest/api/tests/TestAspects.java @@ -27,53 +27,19 @@ package org.alfresco.rest.api.tests; import org.alfresco.repo.security.authentication.AuthenticationUtil; -import org.alfresco.rest.api.tests.client.PublicApiClient; import org.alfresco.rest.api.tests.client.PublicApiException; import org.alfresco.rest.api.tests.client.RequestContext; import org.apache.commons.httpclient.HttpStatus; -import org.junit.Before; import org.junit.Test; -import java.util.HashMap; -import java.util.Map; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -public class TestAspects extends AbstractBaseApiTest +public class TestAspects extends BaseModelApiTest { - - private PublicApiClient.Paging paging = getPaging(0, 10); - PublicApiClient.ListResponse aspects = null; - org.alfresco.rest.api.tests.client.data.Aspect aspect, childAspect = null, smartFilter = null, rescanAspect = null; - Map otherParams = new HashMap<>(); - - @Before - public void setup() throws Exception - { - super.setup(); - - childAspect = new org.alfresco.rest.api.tests.client.data.Aspect(); - childAspect.setId("mycompany:childAspect"); - childAspect.setTitle("Child Aspect"); - childAspect.setDescription("Child Aspect Description"); - childAspect.setParentId("smf:smartFolder"); - - rescanAspect = new org.alfresco.rest.api.tests.client.data.Aspect(); - rescanAspect.setId("test:rescan"); - rescanAspect.setTitle("rescan"); - rescanAspect.setDescription("Doc that required to scan "); - - smartFilter = new org.alfresco.rest.api.tests.client.data.Aspect(); - smartFilter.setId("test:smartFilter"); - smartFilter.setTitle("Smart filter"); - smartFilter.setDescription("Smart Filter"); - smartFilter.setParentId("cm:auditable"); - } - @Test public void testAllAspects() throws PublicApiException { @@ -234,11 +200,4 @@ public class TestAspects extends AbstractBaseApiTest assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode()); } } - - - @Override - public String getScope() - { - return "public"; - } } diff --git a/remote-api/src/test/java/org/alfresco/rest/api/tests/TestTypes.java b/remote-api/src/test/java/org/alfresco/rest/api/tests/TestTypes.java index e8d4d59f59..210f50fc7d 100644 --- a/remote-api/src/test/java/org/alfresco/rest/api/tests/TestTypes.java +++ b/remote-api/src/test/java/org/alfresco/rest/api/tests/TestTypes.java @@ -27,47 +27,18 @@ package org.alfresco.rest.api.tests; import org.alfresco.repo.security.authentication.AuthenticationUtil; -import org.alfresco.rest.api.tests.client.PublicApiClient; import org.alfresco.rest.api.tests.client.PublicApiException; import org.alfresco.rest.api.tests.client.RequestContext; import org.apache.commons.httpclient.HttpStatus; -import org.junit.Before; import org.junit.Test; -import java.util.HashMap; -import java.util.Map; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; - -public class TestTypes extends AbstractBaseApiTest +public class TestTypes extends BaseModelApiTest { - - private PublicApiClient.Paging paging = getPaging(0, 10); - PublicApiClient.ListResponse types = null; - org.alfresco.rest.api.tests.client.data.Type type = null, whitePaperType = null, docType = null; - Map otherParams = new HashMap<>(); - - @Before - public void setup() throws Exception - { - super.setup(); - whitePaperType = new org.alfresco.rest.api.tests.client.data.Type(); - whitePaperType.setId("mycompany:whitepaper"); - whitePaperType.setTitle("whitepaper"); - whitePaperType.setDescription("Whitepaper"); - whitePaperType.setParentId("mycompany:doc"); - - docType = new org.alfresco.rest.api.tests.client.data.Type(); - docType.setId("mycompany:doc"); - docType.setTitle("doc"); - docType.setDescription("Doc"); - docType.setParentId("cm:content"); - } - @Test public void testAllTypes() throws PublicApiException { @@ -181,7 +152,7 @@ public class TestTypes extends AbstractBaseApiTest testListTypeException("(modelIds in ('mycompany:model','unknown:model'))"); testListTypeException("(modelIds in ('unknown:model','unknown1:another'))"); - testListTypeException("(modelIds=' , , ')"); + testListTypeException("(modelIds in (' ', '')"); testListTypeException("(parentIds in ('cm:content','unknown:type')"); testListTypeException("(parentIds in ('unknown:type','cm:content'))"); testListTypeException("(parentIds in ('unknown:type','unknown:types'))"); @@ -228,11 +199,4 @@ public class TestTypes extends AbstractBaseApiTest assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode()); } } - - - @Override - public String getScope() - { - return "public"; - } } diff --git a/remote-api/src/test/java/org/alfresco/rest/api/tests/client/data/Aspect.java b/remote-api/src/test/java/org/alfresco/rest/api/tests/client/data/Aspect.java index a1650bda16..276f62876e 100644 --- a/remote-api/src/test/java/org/alfresco/rest/api/tests/client/data/Aspect.java +++ b/remote-api/src/test/java/org/alfresco/rest/api/tests/client/data/Aspect.java @@ -25,6 +25,7 @@ */ package org.alfresco.rest.api.tests.client.data; +import org.alfresco.rest.api.model.Model; import org.alfresco.rest.api.model.PropertyDefinition; import org.alfresco.rest.api.tests.client.PublicApiClient; import org.json.simple.JSONArray; @@ -51,6 +52,14 @@ public class Aspect extends org.alfresco.rest.api.model.Aspect implements Serial AssertUtil.assertEquals("title", getTitle(), other.getTitle()); AssertUtil.assertEquals("description", getDescription(), other.getDescription()); AssertUtil.assertEquals("parenId", getParentId(), other.getParentId()); + + if (getModel() != null && other.getModel() != null) + { + AssertUtil.assertEquals("modelId", getModel().getId(), other.getModel().getId()); + AssertUtil.assertEquals("author", getModel().getAuthor(), other.getModel().getAuthor()); + AssertUtil.assertEquals("namespaceUri", getModel().getNamespaceUri(), other.getModel().getNamespaceUri()); + AssertUtil.assertEquals("namespacePrefix", getModel().getNamespacePrefix(), other.getModel().getNamespacePrefix()); + } } @SuppressWarnings("unchecked") @@ -79,6 +88,11 @@ public class Aspect extends org.alfresco.rest.api.model.Aspect implements Serial jsonObject.put("properties", getProperties()); } + if (getModel() != null) + { + jsonObject.put("model", getModel()); + } + return jsonObject; } @@ -91,12 +105,21 @@ public class Aspect extends org.alfresco.rest.api.model.Aspect implements Serial String parentId = (String) jsonObject.get("parentId"); List properties = (List) jsonObject.get("properties"); + JSONObject jsonModel = (JSONObject) jsonObject.get("model"); + Model model = new Model(); + model.setId((String) jsonModel.get("id")); + model.setDescription((String) jsonModel.get("description")); + model.setNamespacePrefix((String) jsonModel.get("namespacePrefix")); + model.setNamespaceUri((String) jsonModel.get("namespaceUri")); + model.setAuthor((String) jsonModel.get("author")); + Aspect action = new Aspect(); action.setId(id); action.setTitle(title); action.setDescription(description); action.setParentId(parentId); action.setProperties(properties); + action.setModel(model); return action; } diff --git a/remote-api/src/test/java/org/alfresco/rest/api/tests/client/data/Type.java b/remote-api/src/test/java/org/alfresco/rest/api/tests/client/data/Type.java index 68f77454c6..18fb929fd4 100644 --- a/remote-api/src/test/java/org/alfresco/rest/api/tests/client/data/Type.java +++ b/remote-api/src/test/java/org/alfresco/rest/api/tests/client/data/Type.java @@ -25,6 +25,7 @@ */ package org.alfresco.rest.api.tests.client.data; +import org.alfresco.rest.api.model.Model; import org.alfresco.rest.api.model.PropertyDefinition; import org.alfresco.rest.api.tests.client.PublicApiClient; import org.json.simple.JSONArray; @@ -51,6 +52,14 @@ public class Type extends org.alfresco.rest.api.model.Type implements Serializab AssertUtil.assertEquals("title", getTitle(), other.getTitle()); AssertUtil.assertEquals("description", getDescription(), other.getDescription()); AssertUtil.assertEquals("parenId", getParentId(), other.getParentId()); + + if (getModel() != null && other.getModel() != null) + { + AssertUtil.assertEquals("modelId", getModel().getId(), other.getModel().getId()); + AssertUtil.assertEquals("author", getModel().getAuthor(), other.getModel().getAuthor()); + AssertUtil.assertEquals("namespaceUri", getModel().getNamespaceUri(), other.getModel().getNamespaceUri()); + AssertUtil.assertEquals("namespacePrefix", getModel().getNamespacePrefix(), other.getModel().getNamespacePrefix()); + } } @SuppressWarnings("unchecked") @@ -79,6 +88,11 @@ public class Type extends org.alfresco.rest.api.model.Type implements Serializab jsonObject.put("properties", getProperties()); } + if (getModel() != null) + { + jsonObject.put("model", getModel()); + } + return jsonObject; } @@ -91,12 +105,21 @@ public class Type extends org.alfresco.rest.api.model.Type implements Serializab String parentId = (String) jsonObject.get("parentId"); List properties = (List) jsonObject.get("properties"); + JSONObject jsonModel = (JSONObject) jsonObject.get("model"); + Model model = new Model(); + model.setId((String) jsonModel.get("id")); + model.setDescription((String) jsonModel.get("description")); + model.setNamespacePrefix((String) jsonModel.get("namespacePrefix")); + model.setNamespaceUri((String) jsonModel.get("namespaceUri")); + model.setAuthor((String) jsonModel.get("author")); + Type action = new Type(); action.setId(id); action.setTitle(title); action.setDescription(description); action.setParentId(parentId); action.setProperties(properties); + action.setModel(model); return action; }