From 66e7898b364e9ee45e3250db9815eae9a1382021 Mon Sep 17 00:00:00 2001 From: Sanjay Dalal Date: Mon, 27 Jul 2009 22:32:13 +0000 Subject: [PATCH] CSPACE-267 missing files...intake service refactored to use java or rest apis of nuxeo using new framework test: IntakeServiceTest --- .../intake/nuxeo/IntakeConstants.java | 35 +++ .../nuxeo/IntakeDocumentModelHandler.java | 241 ++++++++++++++++ .../intake/nuxeo/IntakeHandlerFactory.java | 55 ++++ .../nuxeo/IntakeRepresenationHandler.java | 260 ++++++++++++++++++ 4 files changed, 591 insertions(+) create mode 100644 services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeConstants.java create mode 100644 services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeDocumentModelHandler.java create mode 100644 services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeHandlerFactory.java create mode 100644 services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeRepresenationHandler.java diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeConstants.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeConstants.java new file mode 100644 index 000000000..306f7a8e8 --- /dev/null +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeConstants.java @@ -0,0 +1,35 @@ +/** + * 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.intake.nuxeo; + +/** + * IntakeConstants processes CollectionObject document + * + */ +public class IntakeConstants { + + public final static String INTAKE_NUXEO_DOCTYPE = "Intake"; + public final static String INTAKE_NUXEO_SCHEMA_NAME = "intake"; + public final static String INTAKE_NUXEO_DC_TITLE = "CollectionSpace-Intake"; +} diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeDocumentModelHandler.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeDocumentModelHandler.java new file mode 100644 index 000000000..ae87f9684 --- /dev/null +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeDocumentModelHandler.java @@ -0,0 +1,241 @@ +/** + * 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.intake.nuxeo; + +import java.util.Iterator; +import java.util.List; +import org.collectionspace.services.IntakeJAXBSchema; +import org.collectionspace.services.common.repository.DocumentWrapper; +import org.collectionspace.services.intake.Intake; +import org.collectionspace.services.intake.IntakeList; +import org.collectionspace.services.intake.IntakeList.IntakeListItem; +import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler; +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.DocumentModelList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * IntakeDocumentModelHandler + * + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class IntakeDocumentModelHandler + extends DocumentModelHandler { + + private final Logger logger = LoggerFactory.getLogger(IntakeDocumentModelHandler.class); + /** + * intake is used to stash JAXB object to use when handle is called + * for Action.CREATE, Action.UPDATE or Action.GET + */ + private Intake intake; + /** + * intakeList is stashed when handle is called + * for ACTION.GET_ALL + */ + private IntakeList intakeList; + + @Override + public void prepare(Action action) throws Exception { + //no specific action needed + } + + /** + * getCommonObject get associated intake + * @return + */ + @Override + public Intake getCommonObject() { + return intake; + } + + /** + * setCommonObject set associated intake + * @param intake + */ + @Override + public void setCommonObject(Intake intake) { + this.intake = intake; + } + + /** + * getIntakeList get associated intake (for index/GET_ALL) + * @return + */ + @Override + public IntakeList getCommonObjectList() { + return intakeList; + } + + @Override + public void setCommonObjectList(IntakeList intakeList) { + this.intakeList = intakeList; + } + + @Override + public Intake extractCommonObject(DocumentWrapper wrapDoc) + throws Exception { + DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject(); + Intake intakeObject = new Intake(); + + //FIXME property get should be dynamically set using schema inspection + //so it does not require hard coding + + // intake core values + intakeObject.setCurrentOwner((String)docModel.getPropertyValue( + getQProperty(IntakeJAXBSchema.CURRENT_OWNER))); + + intakeObject.setDepositor((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.DEPOSITOR))); + + intakeObject.setDepositorsRequirements((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS))); + + intakeObject.setEntryDate((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_DATE))); + + intakeObject.setEntryMethod((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_METHOD))); + + intakeObject.setEntryNote((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_NOTE))); + + intakeObject.setEntryNumber((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_NUMBER))); + + intakeObject.setEntryReason((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_REASON))); + + intakeObject.setPackingNote((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.PACKING_NOTE))); + + intakeObject.setReturnDate((String)docModel.getPropertyValue(getQProperty( + IntakeJAXBSchema.RETURN_DATE))); + + return intakeObject; + } + + @Override + public void fillCommonObject(Intake intakeObject, DocumentWrapper wrapDoc) throws Exception { + DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject(); + //FIXME property setter should be dynamically set using schema inspection + //so it does not require hard coding + + // a default title for the Dublin Core schema + docModel.setPropertyValue("dublincore:title", IntakeConstants.INTAKE_NUXEO_DC_TITLE); + + // intake core values + if(intakeObject.getCurrentOwner() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.CURRENT_OWNER), intakeObject.getCurrentOwner()); + } + + if(intakeObject.getDepositor() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.DEPOSITOR), intakeObject.getDepositor()); + } + + if(intakeObject.getDepositorsRequirements() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS), intakeObject.getDepositorsRequirements()); + } + + if(intakeObject.getEntryDate() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_DATE), intakeObject.getEntryDate()); + } + + if(intakeObject.getEntryMethod() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_METHOD), intakeObject.getEntryMethod()); + } + + if(intakeObject.getEntryNote() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_NOTE), intakeObject.getEntryNote()); + } + + if(intakeObject.getEntryNumber() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_NUMBER), intakeObject.getEntryNumber()); + } + + if(intakeObject.getEntryReason() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.ENTRY_REASON), intakeObject.getEntryReason()); + } + + if(intakeObject.getPackingNote() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.PACKING_NOTE), intakeObject.getPackingNote()); + } + + if(intakeObject.getReturnDate() != null){ + docModel.setPropertyValue(getQProperty( + IntakeJAXBSchema.RETURN_DATE), intakeObject.getReturnDate()); + } + + } + + @Override + public IntakeList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception { + DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject(); + + IntakeList coList = new IntakeList(); + List list = coList.getIntakeListItem(); + + //FIXME: iterating over a long list of documents is not a long term + //strategy...need to change to more efficient iterating in future + Iterator iter = docList.iterator(); + while(iter.hasNext()){ + DocumentModel docModel = iter.next(); + IntakeListItem ilistItem = new IntakeListItem(); + ilistItem.setEntryNumber((String)docModel.getPropertyValue( + getQProperty(IntakeJAXBSchema.ENTRY_NUMBER))); + //need fully qualified context for URI + String id = docModel.getId(); + ilistItem.setUri("/intakes/" + id); + ilistItem.setCsid(id); + list.add(ilistItem); + } + + return coList; + } + + @Override + public void fillCommonObjectList(IntakeList obj, DocumentWrapper wrapDoc) throws Exception { + throw new UnsupportedOperationException(); + } + + /** + * getQProperty converts the given property to qualified schema property + * @param prop + * @return + */ + private String getQProperty(String prop) { + return IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + prop; + } +} + diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeHandlerFactory.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeHandlerFactory.java new file mode 100644 index 000000000..d5f07828e --- /dev/null +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeHandlerFactory.java @@ -0,0 +1,55 @@ +/** + * 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.intake.nuxeo; + +import org.collectionspace.services.common.NuxeoClientType; +import org.collectionspace.services.common.repository.DocumentHandler; + +/** + * IntakeHandlerFactory creates handlers for collectionobject based + * on type of Nuxeo client used + * + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class IntakeHandlerFactory { + + private static final IntakeHandlerFactory self = new IntakeHandlerFactory(); + + private IntakeHandlerFactory() { + } + + public static IntakeHandlerFactory getInstance() { + return self; + } + + public DocumentHandler getHandler(String clientType) { + if(NuxeoClientType.JAVA.toString().equals(clientType)){ + return new IntakeDocumentModelHandler(); + } else if(NuxeoClientType.REST.toString().equals(clientType)) { + return new IntakeRepresenationHandler(); + } + throw new IllegalArgumentException("Not supported client=" + clientType); + } +} diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeRepresenationHandler.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeRepresenationHandler.java new file mode 100644 index 000000000..720022cfe --- /dev/null +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeRepresenationHandler.java @@ -0,0 +1,260 @@ +/** + * 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.intake.nuxeo; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.collectionspace.services.IntakeJAXBSchema; +import org.collectionspace.services.common.repository.DocumentWrapper; +import org.collectionspace.services.intake.Intake; +import org.collectionspace.services.intake.IntakeList; +import org.collectionspace.services.intake.IntakeList.IntakeListItem; +import org.collectionspace.services.nuxeo.client.rest.RepresentationHandler; +import org.dom4j.Document; +import org.dom4j.Element; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * IntakeDocumentModelHandler + * + * $LastChangedRevision: $ + * $LastChangedDate: $ + */ +public class IntakeRepresenationHandler + extends RepresentationHandler { + + private final Logger logger = LoggerFactory.getLogger(IntakeRepresenationHandler.class); + /** + * intakeObj is used to stash JAXB object to use when handle is called + * for Action.CREATE, Action.UPDATE or Action.GET + */ + private Intake intake; + /** + * intakeListObject is stashed when handle is called + * for ACTION.GET_ALL + */ + private IntakeList intakeList; + + @Override + public void prepare(Action action) throws Exception { + switch(action){ + case CREATE: + case UPDATE: + prepare(); + } + } + + private void prepare() { + Map queryParams = getQueryParams(); + Intake intakeObject = getCommonObject(); + if(intakeObject.getCurrentOwner() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.CURRENT_OWNER, intakeObject.getCurrentOwner()); + } + + if(intakeObject.getDepositor() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.DEPOSITOR, intakeObject.getDepositor()); + } + + if(intakeObject.getDepositorsRequirements() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS, intakeObject.getDepositorsRequirements()); + } + + if(intakeObject.getEntryDate() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.ENTRY_DATE, intakeObject.getEntryDate()); + } + + if(intakeObject.getEntryMethod() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.ENTRY_METHOD, intakeObject.getEntryMethod()); + } + + if(intakeObject.getEntryNote() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.ENTRY_NOTE, intakeObject.getEntryNote()); + } + + if(intakeObject.getEntryNumber() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.ENTRY_NUMBER, intakeObject.getEntryNumber()); + } + + if(intakeObject.getEntryReason() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.ENTRY_REASON, intakeObject.getEntryReason()); + } + + if(intakeObject.getPackingNote() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.PACKING_NOTE, intakeObject.getPackingNote()); + } + + if(intakeObject.getReturnDate() != null){ + queryParams.put(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + + IntakeJAXBSchema.RETURN_DATE, intakeObject.getReturnDate()); + } + } + + @Override + public Intake extractCommonObject(DocumentWrapper wrapDoc) + throws Exception { + Document document = (Document) wrapDoc.getWrappedObject(); + Intake intakeObj = new Intake(); + + //FIXME property get should be dynamically set using schema inspection + //so it does not require hard coding + Element root = document.getRootElement(); + + // TODO: recognize schema thru namespace uri + // Namespace ns = new Namespace("intakeObj", + // "http://collectionspace.org/intakeObj"); + + Iterator siter = root.elementIterator("schema"); + while(siter.hasNext()){ + + Element schemaElement = siter.next(); + if(logger.isDebugEnabled()){ + logger.debug("getCommonObject() populating Common Object"); + } + // TODO: recognize schema thru namespace uri + if(IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME.equals(schemaElement.attribute("name").getValue())){ + Element ele = schemaElement.element(IntakeJAXBSchema.CURRENT_OWNER); + if(ele != null){ + intakeObj.setCurrentOwner((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.DEPOSITOR); + if(ele != null){ + intakeObj.setDepositor((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.DEPOSITORS_REQUIREMENTS); + if(ele != null){ + intakeObj.setDepositorsRequirements((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.ENTRY_DATE); + if(ele != null){ + intakeObj.setEntryDate((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.ENTRY_METHOD); + if(ele != null){ + intakeObj.setEntryMethod((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.ENTRY_NOTE); + if(ele != null){ + intakeObj.setEntryNote((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.ENTRY_NUMBER); + if(ele != null){ + intakeObj.setEntryNumber((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.ENTRY_REASON); + if(ele != null){ + intakeObj.setEntryReason((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.PACKING_NOTE); + if(ele != null){ + intakeObj.setPackingNote((String) ele.getData()); + } + ele = schemaElement.element(IntakeJAXBSchema.RETURN_DATE); + if(ele != null){ + intakeObj.setReturnDate((String) ele.getData()); + } + } + } + return intakeObj; + } + + @Override + public void fillCommonObject(Intake co, DocumentWrapper wrapDoc) + throws Exception { + //Nuxeo REST takes create/update through queryParams, nothing to do here + } + + @Override + public IntakeList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception { + Document document = (Document) wrapDoc.getWrappedObject(); + if(logger.isDebugEnabled()){ + logger.debug(document.asXML()); + } + IntakeList intakeListObject = new IntakeList(); + List list = intakeListObject.getIntakeListItem(); + Element root = document.getRootElement(); + for(Iterator i = root.elementIterator(); i.hasNext();){ + + Element element = (Element) i.next(); + if(logger.isDebugEnabled()){ + logger.debug(element.asXML()); + } + // set the intakeObj list item entity elements + IntakeListItem ilistItem = new IntakeListItem(); + ilistItem.setEntryNumber(element.attributeValue("entryNumber")); + String id = element.attributeValue("id"); + ilistItem.setCsid(id); + ilistItem.setUri("/intakes/" + id); + list.add(ilistItem); + } + return intakeListObject; + } + + @Override + public void fillCommonObjectList(IntakeList obj, DocumentWrapper wrapDoc) + throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public Intake getCommonObject() { + return intake; + } + + @Override + public void setCommonObject(Intake obj) { + this.intake = obj; + } + + @Override + public IntakeList getCommonObjectList() { + return intakeList; + } + + @Override + public void setCommonObjectList(IntakeList obj) { + this.intakeList = obj; + } + + /** + * getQProperty converts the given property to qualified schema property + * @param prop + * @return + */ + private String getQProperty(String prop) { + return IntakeConstants.INTAKE_NUXEO_SCHEMA_NAME + ":" + prop; + } +} + -- 2.47.3