From 29307d4eb1ac7e33428c585b5f27e9f2a5637c67 Mon Sep 17 00:00:00 2001 From: Laramie Crocker Date: Wed, 15 Dec 2010 21:10:03 +0000 Subject: [PATCH] CSPACE-3332 --- .../nuxeo/client/java/DocHandlerBase.java | 252 ++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100755 services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java new file mode 100755 index 000000000..24e5bba32 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java @@ -0,0 +1,252 @@ +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * 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.collectionspace.services.nuxeo.client.java; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.collectionspace.services.common.ReflectionMapper; +import org.collectionspace.services.common.Tools; +import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.jaxb.AbstractCommonList; +import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl; +import org.collectionspace.services.nuxeo.util.NuxeoUtils; +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.DocumentModelList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is generified by the marker types T and TL, + * however, T is expected to map to something like BlobCommon, MediaCommon, ObjectexitCommon, etc., + * whereas TL is expected to map to AbstractCommonList, + * since, for example, BlobCommonList and ObjectexitCommonList descend from AbstractCommonList, + * and so on for every JAXB-generated schema class. + * + * User: laramie + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + */ +public abstract class DocHandlerBase extends RemoteDocumentModelHandlerImpl { + + /** The logger. */ + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private AbstractCommonList commonList; + + @Override + public TL getCommonPartList() { + return (TL)commonList; + } + + public void setCommonPartList(AbstractCommonList aCommonList) { + this.commonList = aCommonList; + } + + private T commonPart; + + @Override + public T getCommonPart() { + return (T)commonPart; + } + + public void setCommonPart(T commonPart) { + this.commonPart = commonPart; + } + + /** Subclass DocHandlers may override this method to control exact creation of the common list. + * This class instantiates an AbstractCommonList from the classname returned by getCommonListReflection().AbstractCommonListClassname. + * @return + * @throws Exception + */ + public AbstractCommonList createAbstractCommonListImpl() throws Exception { + // String classname = this.commonList.getClass().getName(); + String classname = getAbstractCommonListClassname(); + return (AbstractCommonList)(ReflectionMapper.instantiate(classname)); + } + + + /** DocHandlerBase calls this method with the CSID as id */ + public Object createItemForCommonList(DocumentModel docModel, String label, String id) throws Exception { + return createItemForCommonList(getCommonListItemClassname(), docModel, label, id, true); + } + + public static class CommonListReflection { + public String NuxeoSchemaName; + public String DublinCoreTitle; // TODO: for CollectionObjectDocumentModelHandler, NUXEO_DC_TITLE = "CollectionSpace-CollectionObject" + public String SummaryFields; + public String AbstractCommonListClassname; + public String CommonListItemClassname; + public String ListItemMethodName; + public String[][] ListItemsArray; //setter, element, container, sub-element + public static int SETTER=0, ELEMENT=1, CONTAINER=2, SUBELEMENT=3; + } + + public abstract CommonListReflection getCommonListReflection(); + + public String getNuxeoSchemaName(){ + return getCommonListReflection().NuxeoSchemaName; + } + + public String getSummaryFields(AbstractCommonList commonList){ + return getCommonListReflection().SummaryFields; + } + + public String getAbstractCommonListClassname(){ + return getCommonListReflection().AbstractCommonListClassname; + } + + public String getCommonListItemClassname(){ + return getCommonListReflection().CommonListItemClassname; + } + + public String[][] getListItemsArray(){ + return getCommonListReflection().ListItemsArray; + } + + + + @Override + public T extractCommonPart(DocumentWrapper wrapDoc) throws Exception { + throw new UnsupportedOperationException(); + } + + public void fillCommonPart(T objectexitObject, DocumentWrapper wrapDoc) throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public TL extractCommonPartList(DocumentWrapper wrapDoc) throws Exception { + String label = getServiceContext().getCommonPartLabel(); + + AbstractCommonList commonList = createAbstractCommonListImpl(); + extractPagingInfo(((TL)commonList), wrapDoc); + commonList.setFieldsReturned(getSummaryFields(commonList)); + List list = createItemsList(commonList); + Iterator iter = wrapDoc.getWrappedObject().iterator(); + while(iter.hasNext()){ + DocumentModel docModel = iter.next(); + String id = NuxeoUtils.extractId(docModel.getPathAsString()); + Object item = createItemForCommonList(docModel, label, id); + list.add(item); + } + return (TL)commonList; + } + + @Override + public String getQProperty(String prop) { + return getNuxeoSchemaName() + ":" + prop; + } + + //============= dublin core handling ======================================= + + @Override + public void fillAllParts(DocumentWrapper wrapDoc, Action action) throws Exception { + super.fillAllParts(wrapDoc, action); + fillDublinCoreObject(wrapDoc); + } + + protected void fillDublinCoreObject(DocumentWrapper wrapDoc) throws Exception { + String title = getCommonListReflection().DublinCoreTitle; + if (Tools.isEmpty(title)){ + return; + } + DocumentModel docModel = wrapDoc.getWrappedObject(); + docModel.setPropertyValue("dublincore:title", title); + } + + //================== UTILITY METHODS ================================================ + + public static ReflectionMapper.STATUS callSetter(DocumentModel docModel, + Object listItem, + String label, + String id, + String setterName, + String elementName) + throws Exception { + Object prop = docModel.getProperty(label, elementName); + return ReflectionMapper.callSetter(listItem, setterName, prop); + } + + public static ReflectionMapper.STATUS callSetter(Object target, String name, Object arg) { + return ReflectionMapper.callSetter(target, name, arg); + } + + /** @param commonListClassname is a package-qualified java classname, including inner class $ notation, such as + * "org.collectionspace.services.objectexit.ObjectexitCommonList$ObjectexitListItem". + * @param includeStdFields set to true to have the method set Uri and Csid automatically, based on id param. + */ + public Object createItemForCommonList(String commonListClassname, DocumentModel docModel, String label, String id, boolean includeStdFields) throws Exception { + //createItemForCommonList(docModel, label, id); + Object item = ReflectionMapper.instantiate(commonListClassname); + String [][] names = getListItemsArray(); + for (String[] row : names){ + if (row.length >2 + && Tools.notEmpty(row[CommonListReflection.CONTAINER]) + && Tools.notEmpty(row[CommonListReflection.SUBELEMENT])){ + String container = row[CommonListReflection.CONTAINER];//"lenderGroupList"; //LoaninListItemJAXBSchema.LENDER_GROUP_LIST; + String element = row[CommonListReflection.SUBELEMENT];//"lender"; //LoaninListItemJAXBSchema.LENDER; + String setter = row[CommonListReflection.SETTER]; + List listOfMulti = (List) docModel.getProperty(label, container); + String primary = primaryValueFromMultivalue(listOfMulti, element); + callSetter(item, setter, primary); //"Lender" //item.setLender(primary); + } else { + callSetter(docModel, item, label, id, row[CommonListReflection.SETTER], row[CommonListReflection.ELEMENT]); + } + } + if (includeStdFields){ + callSetter(item, "Csid", id); + callSetter(item, "Uri", getServiceContextPath() + id); + } + return item; + } + + /** Subclasses should override this method if they don't want to automatically + * call List createItemsList(AbstractCommonList commonList, String listItemMethodName) + * which will use introspection to create a summary list, and will find the primary + * field for you if specified. + */ + public List createItemsList(AbstractCommonList commonList) throws Exception { + return createItemsList(commonList, getCommonListReflection().ListItemMethodName); + } + + /** e.g. createItemsList(commonList, "getObjectexitListItem" */ + public List createItemsList(AbstractCommonList commonList, String listItemMethodName) throws Exception { + Class commonListClass = commonList.getClass(); + Class[] types = new Class[] {}; + try { + Method m = commonListClass.getMethod(listItemMethodName, types); + return (List)(ReflectionMapper.fireGetMethod(m, commonList)); + } catch (NoSuchMethodException nsm){ + return new ArrayList(); + } + } + + + +} + -- 2.47.3