From 701f39d4d5a2420e6f041a06c6068a67b1bea91b Mon Sep 17 00:00:00 2001 From: Tom Page Date: Mon, 12 Oct 2015 10:35:19 +0000 Subject: [PATCH] RM-2604 Implementation of JSON-based caveat DAO. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/DEV/caveatmarkdatatype@114109 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../caveat/CaveatException.java | 17 ++ .../caveat/dao/CaveatDAOFromJSON.java | 157 ++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/CaveatException.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/CaveatException.java index fd2fd60bc9..6777be24ed 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/CaveatException.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/CaveatException.java @@ -53,4 +53,21 @@ public class CaveatException extends AlfrescoRuntimeException super("Could not find caveat mark with id " + caveatMarkId); } } + + /** The caveat configuration file contains errors. */ + public static class MalformedConfiguration extends CaveatException + { + /** serial version uid */ + private static final long serialVersionUID = -4486048933410071424L; + + public MalformedConfiguration(String message) + { + super(message); + } + + public MalformedConfiguration(String message, Throwable cause) + { + super(message, cause); + } + } } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java new file mode 100644 index 0000000000..63b441fe6a --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * 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 . + */ + +package org.alfresco.module.org_alfresco_module_rm.caveat.dao; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.MalformedConfiguration; +import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; +import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroupType; +import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatMark; +import org.apache.commons.io.IOUtils; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONTokener; + +/** + * An object that provides access to the configured caveat groups and marks, which it retrieves from JSON files. + * + * @author Tom Page + * @since 2.4.a + */ +public class CaveatDAOFromJSON implements CaveatDAOInterface +{ + /** JSON key for the group id. */ + private static final String GROUP_ID_JSON_KEY = "id"; + /** JSON key for the group display label key. */ + private static final String GROUP_DISPLAY_LABEL_JSON_KEY = "displayLabel"; + /** JSON key for the group description key. */ + private static final String DESCRIPTION_JSON_KEY = "description"; + /** JSON key for the group type. */ + private static final String TYPE_JSON_KEY = "type"; + /** JSON key for the caveat marks array. */ + private static final String MARKS_JSON_KEY = "marks"; + /** JSON key for the mark id. */ + private static final String MARK_ID_JSON_KEY = "id"; + /** JSON key for the mark display label key. */ + private static final String MARK_DISPLAY_LABEL_JSON_KEY = "displayLabel"; + + /** The location of the configuration file relative to the classpath. */ + String configLocation; + + /** Set the location of the configuration file relative to the classpath. */ + public void setConfigLocation(String configLocation) + { + this.configLocation = configLocation; + } + + /** + * {@inheritDoc} + * + * @throws MalformedConfiguration If the configuration file cannot be interpreted. + */ + @Override + public Map getCaveatGroups() + { + Map result = new HashMap<>(); + try (final InputStream in = this.getClass().getResourceAsStream(configLocation)) + { + if (in != null) + { + final String jsonString = IOUtils.toString(in); + final JSONArray jsonArray = new JSONArray(new JSONTokener(jsonString)); + + for (int i = 0; i < jsonArray.length(); i++) + { + final JSONObject nextObj = jsonArray.getJSONObject(i); + CaveatGroup caveatGroup = createGroup(nextObj); + String caveatGroupId = caveatGroup.getId(); + if (result.containsKey(caveatGroupId)) { throw new MalformedConfiguration( + "Configuration contains two caveat groups with id " + caveatGroupId); } + result.put(caveatGroupId, caveatGroup); + } + } + } + catch (IOException | JSONException e) + { + String message = "Could not read caveat configuration: " + configLocation; + throw new MalformedConfiguration(message, e); + } + return result; + } + + /** + * Create a caveat group from the supplied JSON. + * + * @param jsonGroup The JSON object corresponding to a single group and its marks. + * @return The created group. + * @throws JSONException If there is an issue reading the JSON. + */ + private CaveatGroup createGroup(JSONObject jsonGroup) throws JSONException + { + String id = jsonGroup.getString(GROUP_ID_JSON_KEY); + String displayLabelKey = jsonGroup.getString(GROUP_DISPLAY_LABEL_JSON_KEY); + String descriptionKey = jsonGroup.getString(DESCRIPTION_JSON_KEY); + String caveatGroupTypeString = jsonGroup.getString(TYPE_JSON_KEY); + CaveatGroupType caveatGroupType; + try + { + caveatGroupType = CaveatGroupType.valueOf(caveatGroupTypeString); + } + catch (IllegalArgumentException e) + { + throw new MalformedConfiguration("Unrecognised caveat group type " + caveatGroupTypeString, e); + } + + // Create a list of the configured caveat marks. + List caveatMarks = new ArrayList<>(); + JSONArray jsonMarks = jsonGroup.getJSONArray(MARKS_JSON_KEY); + for (int i = 0; i < jsonMarks.length(); i++) + { + JSONObject jsonMark = jsonMarks.getJSONObject(i); + caveatMarks.add(createMark(jsonMark)); + } + + // Instantiate the group (and associate the marks with the group). + CaveatGroup caveatGroup = new CaveatGroup(id, displayLabelKey, descriptionKey, caveatGroupType, caveatMarks); + + return caveatGroup; + } + + /** + * Create a caveat mark from the supplied JSON. This does not set the group id of the caveat mark. + * + * @param jsonMark The JSON object corresponding to a single mark. + * @return The created mark. + * @throws JSONException If there is an issue reading the JSON. + */ + private CaveatMark createMark(JSONObject jsonMark) throws JSONException + { + String id = jsonMark.getString(MARK_ID_JSON_KEY); + String displayLabelKey = jsonMark.getString(MARK_DISPLAY_LABEL_JSON_KEY); + return new CaveatMark(id, displayLabelKey); + } +}