From: Sanjay Dalal Date: Tue, 24 Nov 2009 21:02:00 +0000 (+0000) Subject: CSPACE-308, CSPACE-309, CSPACE-585, CSPACE-582, CSPACE-615 Account Service is X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=199e0c549d4a53a477f7a77433c6f201799321e3;p=tmp%2Fjakarta-migration.git CSPACE-308, CSPACE-309, CSPACE-585, CSPACE-582, CSPACE-615 Account Service is functional. Service consumer could create, get, update and delete an account. index operation also works. Running mvn -Pddl in account/client generates account.sql using hibernate tool with the help of Jpa annotations. JpaStorage is functional as well. Added a property named entity-name to service context to indicate Jpa entity for data base operations. NOTE!!!! Before running tests, a one time execution of 'ant jpa' is required from services/common. Also requires deploy at trunk level as cspace datasource configuration has changed. tests: mvn test passes (requires deploy (trunk) and jpa (common)) _M services/authentication/jaxb M services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java M services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml M services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml M services/JaxRsServiceProvider/pom.xml A services/common/lib A services/common/lib/jpa-upgrade AM services/common/lib/jpa-upgrade/ejb3-persistence-1.0.2.GA.jar AM services/common/lib/jpa-upgrade/hibernate-entitymanager-3.4.0.GA.jar AM services/common/lib/jpa-upgrade/hibernate-annotations-3.4.0.GA.jar A services/common/lib/README.txt M services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java M services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java M services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java M services/common/build.xml M services/pom.xml M services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java M services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java A + services/account/service/src/main/resources/META-INF M services/account/service/src/main/resources/META-INF/persistence.xml D services/account/jaxb/src/test/java/org/collectionspace/services/account/test/AccountTest.java D services/account/jaxb/src/test/resources/META-INF D services/account/jaxb/src/test/resources/hibernate.cfg.xml D services/account/jaxb/src/main/resources/META-INF M services/account/jaxb/src/main/resources/accounts_common.xsd D services/account/jaxb/src/main/resources/db/mysql/account.sql M services/account/jaxb/pom.xml D services/account/3rdparty M services/account/pom.xml M services/account/build.xml A + services/account/client/src/test/java/org/collectionspace/services/client/test/AccountTest.java M services/account/client/src/test/java/org/collectionspace/services/client/test/AccountServiceTest.java A + services/account/client/src/test/resources/META-INF A + services/account/client/src/test/resources/hibernate.cfg.xml A services/account/client/src/main/resources/db A services/account/client/src/main/resources/db/mysql A services/account/client/src/main/resources/db/mysql/account.sql M services/account/client/pom.xml M src/main/resources/config/cspace-ds.xml --- diff --git a/services/JaxRsServiceProvider/pom.xml b/services/JaxRsServiceProvider/pom.xml index 4c57e39c6..c57d1261a 100644 --- a/services/JaxRsServiceProvider/pom.xml +++ b/services/JaxRsServiceProvider/pom.xml @@ -22,11 +22,11 @@ org.collectionspace.services.common 1.0 - + org.collectionspace.services org.collectionspace.services.collectionobject.service diff --git a/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java b/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java index 406b69d94..c7196a3dd 100644 --- a/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java +++ b/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java @@ -1,6 +1,6 @@ package org.collectionspace.services.jaxrs; -//import org.collectionspace.services.account.AccountResource; +import org.collectionspace.services.account.AccountResource; import org.collectionspace.services.collectionobject.CollectionObjectResource; import org.collectionspace.services.id.IDResource; import org.collectionspace.services.intake.IntakeResource; @@ -22,7 +22,7 @@ public class CollectionSpaceJaxRsApplication extends Application { private Set> empty = new HashSet>(); public CollectionSpaceJaxRsApplication() { -// singletons.add(new AccountResource()); + singletons.add(new AccountResource()); singletons.add(new CollectionObjectResource()); singletons.add(new IDResource()); singletons.add(new IntakeResource()); diff --git a/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml b/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml index 10a4024eb..2dddc9661 100644 --- a/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml +++ b/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/jboss-web.xml @@ -3,11 +3,11 @@ java:/jaas/cspace /cspace-services - + diff --git a/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml b/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml index 62d9b25b9..5660b37af 100644 --- a/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml +++ b/services/JaxRsServiceProvider/src/main/webapp/WEB-INF/web.xml @@ -66,10 +66,10 @@ CollectionSpace realm END AUTH --> - + diff --git a/services/account/3rdparty/build.xml b/services/account/3rdparty/build.xml deleted file mode 100644 index c0a1b94bd..000000000 --- a/services/account/3rdparty/build.xml +++ /dev/null @@ -1,126 +0,0 @@ - - - - account service 3rd party - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/services/account/3rdparty/pom.xml b/services/account/3rdparty/pom.xml deleted file mode 100644 index a4a7a16f7..000000000 --- a/services/account/3rdparty/pom.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - org.collectionspace.services - org.collectionspace.services.account - 1.0 - - - 4.0.0 - org.collectionspace.services - 1.0 - org.collectionspace.services.account.3rdparty - pom - services.account.3rdparty - - 3rd party build for account service - - - - - - - diff --git a/services/account/build.xml b/services/account/build.xml index 8219b1980..445894e2f 100644 --- a/services/account/build.xml +++ b/services/account/build.xml @@ -1,5 +1,5 @@ - + collectionobject service diff --git a/services/account/client/pom.xml b/services/account/client/pom.xml index c350fe477..eb2b3786e 100644 --- a/services/account/client/pom.xml +++ b/services/account/client/pom.xml @@ -14,7 +14,10 @@ org.collectionspace.services.account.client 1.0 services.account.client - + + account.sql + ${basedir}/src/main/resources/db/mysql + @@ -79,6 +82,12 @@ commons-httpclient 3.1 + + + + mysql + mysql-connector-java + @@ -106,5 +115,67 @@ + + + + ddl + + + + org.codehaus.mojo + hibernate3-maven-plugin + 2.2 + + + + hbm2ddl + + + + + + + hbm2ddl + + + + ${sql.file} + jpaconfiguration + true + true + true + true + false + org.collectionspace.services.account + + + + + mysql + mysql-connector-java + 5.0.5 + + + + + maven-antrun-plugin + + + + + + + + + run + + + + + + + + + diff --git a/services/account/client/src/main/resources/db/mysql/account.sql b/services/account/client/src/main/resources/db/mysql/account.sql new file mode 100644 index 000000000..4ad291461 --- /dev/null +++ b/services/account/client/src/main/resources/db/mysql/account.sql @@ -0,0 +1,2 @@ +drop table if exists accounts_common; +create table accounts_common (csid varchar(255) not null, anchor_name varchar(128) not null, email longtext not null, first_name longtext not null, last_name longtext not null, mi varchar(1), mobile varchar(15), phone varchar(15), primary key (csid)); diff --git a/services/account/client/src/test/java/org/collectionspace/services/client/test/AccountServiceTest.java b/services/account/client/src/test/java/org/collectionspace/services/client/test/AccountServiceTest.java index 881d93437..f009e76e1 100644 --- a/services/account/client/src/test/java/org/collectionspace/services/client/test/AccountServiceTest.java +++ b/services/account/client/src/test/java/org/collectionspace/services/client/test/AccountServiceTest.java @@ -31,7 +31,6 @@ import org.collectionspace.services.account.AccountsCommon; import org.collectionspace.services.account.AccountsCommonList; import org.jboss.resteasy.client.ClientResponse; -import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; import org.testng.Assert; import org.testng.annotations.Test; @@ -76,7 +75,7 @@ public class AccountServiceTest extends AbstractServiceTest { // Submit the request to the service and store the response. AccountsCommon account = - createAccountInstance("sanjay", "hello"); + createAccountInstance("barney", "dino", "barney", "hello", "barney@dinoland.com"); ClientResponse res = client.create(account); int statusCode = res.getStatus(); @@ -207,7 +206,7 @@ public class AccountServiceTest extends AbstractServiceTest { Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); // Optionally output additional data about list members for debugging. - boolean iterateThroughList = false; + boolean iterateThroughList = true; if (iterateThroughList && logger.isDebugEnabled()) { List items = list.getAccountListItem(); @@ -234,7 +233,7 @@ public class AccountServiceTest extends AbstractServiceTest { // Success outcomes @Override @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class, - dependsOnMethods = {"read"}) + dependsOnMethods = {"read", "readNonExistent"}) public void update(String testName) throws Exception { // Perform setup. @@ -250,10 +249,8 @@ public class AccountServiceTest extends AbstractServiceTest { if (logger.isDebugEnabled()) { logger.debug("got object to update with ID: " + knownResourceId); } - MultipartInput input = (MultipartInput) res.getEntity(); AccountsCommon toUpdateAccount = - (AccountsCommon) extractPart(input, - client.getCommonPartName(), AccountsCommon.class); + (AccountsCommon) res.getEntity(); Assert.assertNotNull(toUpdateAccount); // Update the content of this resource. @@ -277,11 +274,11 @@ public class AccountServiceTest extends AbstractServiceTest { Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); - AccountsCommon updatedAccount = (AccountsCommon)res.getEntity(); + AccountsCommon updatedAccount = (AccountsCommon) res.getEntity(); Assert.assertNotNull(updatedAccount); Assert.assertEquals(updatedAccount.getEmail(), - toUpdateAccount.getPhone(), + toUpdateAccount.getEmail(), "Data in updated object did not match submitted data."); } @@ -303,7 +300,7 @@ public class AccountServiceTest extends AbstractServiceTest { @Override @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class, - dependsOnMethods = {"update", "testSubmitRequest"}) + dependsOnMethods = {"update", "readNonExistent", "testSubmitRequest"}) public void updateNonExistent(String testName) throws Exception { // Perform setup. @@ -314,7 +311,7 @@ public class AccountServiceTest extends AbstractServiceTest { // Note: The ID used in this 'create' call may be arbitrary. // The only relevant ID may be the one used in updateAccount(), below. AccountsCommon account = - createAccountInstance("dalal", "junk"); + createAccountInstance("simba", "mufasa", "simba", "tiger", "simba@lionking.com"); ClientResponse res = client.update(NON_EXISTENT_ID, account); int statusCode = res.getStatus(); @@ -335,7 +332,7 @@ public class AccountServiceTest extends AbstractServiceTest { // Success outcomes @Override @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class, - dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"}) + dependsOnMethods = {"create", "readList", "testSubmitRequest", "update", "updateNonExistent"}) public void delete(String testName) throws Exception { // Perform setup. @@ -409,15 +406,17 @@ public class AccountServiceTest extends AbstractServiceTest { // --------------------------------------------------------------- // Utility methods used by tests above // --------------------------------------------------------------- - private AccountsCommon createAccountInstance(String anchorName, - String passwd) { + private AccountsCommon createAccountInstance(String firstName, String lastName, String anchorName, + String passwd, String email) { AccountsCommon account = new AccountsCommon(); + account.setFirstName(firstName); + account.setLastName(lastName); account.setAnchorName(anchorName); account.setUserName(anchorName); byte[] b64passwd = Base64.encodeBase64(passwd.getBytes()); account.setPassword(b64passwd); - + account.setEmail(email); if (logger.isDebugEnabled()) { logger.debug("to be created, account common"); logger.debug(objectAsXmlString(account, @@ -426,5 +425,4 @@ public class AccountServiceTest extends AbstractServiceTest { return account; } - } diff --git a/services/account/jaxb/src/test/java/org/collectionspace/services/account/test/AccountTest.java b/services/account/client/src/test/java/org/collectionspace/services/client/test/AccountTest.java similarity index 92% rename from services/account/jaxb/src/test/java/org/collectionspace/services/account/test/AccountTest.java rename to services/account/client/src/test/java/org/collectionspace/services/client/test/AccountTest.java index 31ddfaa05..e6b77b9f9 100644 --- a/services/account/jaxb/src/test/java/org/collectionspace/services/account/test/AccountTest.java +++ b/services/account/client/src/test/java/org/collectionspace/services/client/test/AccountTest.java @@ -2,7 +2,7 @@ * To change this template, choose Tools | Templates * and open the template in the editor. */ -package org.collectionspace.services.account.test; +package org.collectionspace.services.client.test; import java.lang.reflect.Method; import java.util.List; @@ -56,10 +56,10 @@ public class AccountTest { @Test(dataProvider = "testName", dataProviderClass = AccountTest.class) public void create(String testName) throws Exception { AccountsCommon account = new AccountsCommon(); - account.setAnchorName("sanjay"); - account.setFirstName("Sanjay"); - account.setLastName("Dalal"); - account.setEmail("sanjay.dalal@berkeley.edu"); + account.setAnchorName("john"); + account.setFirstName("John"); + account.setLastName("Doe"); + account.setEmail("john.doe@berkeley.edu"); id = UUID.randomUUID().toString(); account.setCsid(id); em.getTransaction().begin(); @@ -77,7 +77,7 @@ public class AccountTest { @Test(dataProvider = "testName", dataProviderClass = AccountTest.class, dependsOnMethods = {"create"}) public void read(String testName) throws Exception { - AccountsCommon account = findAccount("sanjay"); + AccountsCommon account = findAccount("john"); Assert.assertNotNull(account); if (logger.isDebugEnabled()) { logger.debug("read account " + @@ -97,14 +97,14 @@ public class AccountTest { dependsOnMethods = {"read"}) public void update(String testName) throws Exception { Query q = em.createQuery("update org.collectionspace.services.account.AccountsCommon set email= :email where csid=:csid"); - q.setParameter("email", "sanjay@berkeley.edu"); + q.setParameter("email", "john@berkeley.edu"); q.setParameter("csid", id); em.getTransaction().begin(); int no = q.executeUpdate(); // Commit the transaction em.getTransaction().commit(); Assert.assertEquals(no, 1); - AccountsCommon account = findAccount("sanjay"); + AccountsCommon account = findAccount("john"); if (logger.isDebugEnabled()) { logger.debug("updated account " + " first name=" + account.getFirstName() + diff --git a/services/account/jaxb/src/test/resources/META-INF/persistence.xml b/services/account/client/src/test/resources/META-INF/persistence.xml similarity index 100% rename from services/account/jaxb/src/test/resources/META-INF/persistence.xml rename to services/account/client/src/test/resources/META-INF/persistence.xml diff --git a/services/account/jaxb/src/test/resources/hibernate.cfg.xml b/services/account/client/src/test/resources/hibernate.cfg.xml similarity index 100% rename from services/account/jaxb/src/test/resources/hibernate.cfg.xml rename to services/account/client/src/test/resources/hibernate.cfg.xml diff --git a/services/account/jaxb/pom.xml b/services/account/jaxb/pom.xml index 773625734..62cf46e98 100644 --- a/services/account/jaxb/pom.xml +++ b/services/account/jaxb/pom.xml @@ -14,10 +14,6 @@ org.collectionspace.services.account.jaxb 1.0 services.account.jaxb - - account.sql - src/main/resources/db/mysql - @@ -87,6 +83,7 @@ true true false + garbage @@ -96,73 +93,8 @@ 1.6 - - - - - ddl - - - - org.codehaus.mojo - hibernate3-maven-plugin - 2.2 - - - - process-test-resources - - hbm2ddl - - - - - - - hbm2ddl - - - - ${sql.file} - jpaconfiguration - true - true - true - true - false - org.collectionspace.services.account - - - - - mysql - mysql-connector-java - 5.0.5 - - - - - maven-antrun-plugin - - - process-test-classes - - - - - - - run - - - - - - - - diff --git a/services/account/jaxb/src/main/resources/accounts_common.xsd b/services/account/jaxb/src/main/resources/accounts_common.xsd index 8d4027138..8eca84f96 100644 --- a/services/account/jaxb/src/main/resources/accounts_common.xsd +++ b/services/account/jaxb/src/main/resources/accounts_common.xsd @@ -32,23 +32,100 @@ + + + + + + + - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + - + + + + + + + - + + + @@ -57,9 +134,19 @@ + + + + + + + + + + diff --git a/services/account/jaxb/src/main/resources/db/mysql/account.sql b/services/account/jaxb/src/main/resources/db/mysql/account.sql deleted file mode 100644 index e69de29bb..000000000 diff --git a/services/account/pom.xml b/services/account/pom.xml index b739055ce..15ce11de5 100644 --- a/services/account/pom.xml +++ b/services/account/pom.xml @@ -15,7 +15,6 @@ jaxb service - 3rdparty client diff --git a/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java b/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java index 933f1b606..e44155e69 100644 --- a/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java +++ b/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java @@ -69,7 +69,8 @@ public class AccountResource private ServiceContext createServiceContext(T obj) { ServiceContext ctx = new RemoteServiceContextImpl(getServiceName()); ctx.setInput(obj); - ctx.setDocumentType("org.collectionspace.services.account"); //persistence unit + ctx.setDocumentType(AccountsCommon.class.getPackage().getName()); //persistence unit + ctx.setProperty("entity-name", AccountsCommon.class.getName()); return ctx; } @@ -127,7 +128,7 @@ public class AccountResource ServiceContext ctx = createServiceContext((AccountsCommon) null); DocumentHandler handler = createDocumentHandler(ctx); getStorageClient(ctx).get(ctx, csid, handler); - result = (AccountsCommon) ctx.getOutput(); + result = (AccountsCommon)ctx.getOutput(); } catch (DocumentNotFoundException dnfe) { if (logger.isDebugEnabled()) { logger.debug("getAccount", dnfe); @@ -164,7 +165,7 @@ public class AccountResource DocumentFilter myFilter = new DocumentFilter(); handler.setDocumentFilter(myFilter); getStorageClient(ctx).getFiltered(ctx, handler); - accountList = null; + accountList = (AccountsCommonList)handler.getCommonPartList(); } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("Caught exception in getAccountList", e); @@ -196,7 +197,7 @@ public class AccountResource ServiceContext ctx = createServiceContext(theUpdate); DocumentHandler handler = createDocumentHandler(ctx); getStorageClient(ctx).update(ctx, csid, handler); - result = (AccountsCommon) ctx.getOutput(); + result = (AccountsCommon)ctx.getOutput(); } catch (DocumentNotFoundException dnfe) { if (logger.isDebugEnabled()) { logger.debug("caugth exception in updateAccount", dnfe); diff --git a/services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java b/services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java index 5e4a677f5..a42b50bbb 100644 --- a/services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java +++ b/services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountDocumentHandler.java @@ -23,18 +23,21 @@ */ package org.collectionspace.services.account.storage; +import java.util.List; import java.util.UUID; import org.collectionspace.services.account.AccountsCommon; import org.collectionspace.services.account.AccountsCommonList; +import org.collectionspace.services.account.AccountsCommonList.AccountListItem; import org.collectionspace.services.common.document.AbstractDocumentHandler; import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.nuxeo.util.NuxeoUtils; /** * * @author */ public class AccountDocumentHandler - extends AbstractDocumentHandler { + extends AbstractDocumentHandler { private AccountsCommon account; private AccountsCommonList accountList; @@ -48,22 +51,27 @@ public class AccountDocumentHandler @Override public void handleUpdate(DocumentWrapper wrapDoc) throws Exception { + getServiceContext().setOutput(getCommonPart()); } @Override public void handleGet(DocumentWrapper wrapDoc) throws Exception { - setCommonPart(wrapDoc.getWrappedObject()); + setCommonPart(extractCommonPart(wrapDoc)); + getServiceContext().setOutput(getCommonPart()); } @Override - public void handleGetAll(DocumentWrapper wrapDoc) throws Exception { - setCommonPartList(wrapDoc.getWrappedObject()); + public void handleGetAll(DocumentWrapper wrapDoc) throws Exception { + AccountsCommonList accList = extractCommonPartList(wrapDoc); + setCommonPartList(accList); + getServiceContext().setOutput(getCommonPartList()); } @Override - public AccountsCommon extractCommonPart(DocumentWrapper wrapDoc) + public AccountsCommon extractCommonPart( + DocumentWrapper wrapDoc) throws Exception { - throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler"); + return wrapDoc.getWrappedObject(); } @Override @@ -73,9 +81,26 @@ public class AccountDocumentHandler } @Override - public AccountsCommonList extractCommonPartList(DocumentWrapper wrapDoc) + public AccountsCommonList extractCommonPartList( + DocumentWrapper wrapDoc) throws Exception { - return wrapDoc.getWrappedObject(); + + AccountsCommonList accList = new AccountsCommonList(); + List list = accList.getAccountListItem(); + + for (Object obj : wrapDoc.getWrappedObject()) { + AccountsCommon account = (AccountsCommon) obj; + AccountListItem accListItem = new AccountListItem(); + accListItem.setAnchorName(account.getAnchorName()); + accListItem.setEmail(account.getEmail()); + accListItem.setFirstName(account.getFirstName()); + accListItem.setLastName(account.getLastName()); + String id = account.getCsid(); + accListItem.setUri(getServiceContextPath() + id); + accListItem.setCsid(id); + list.add(accListItem); + } + return accList; } @Override @@ -99,7 +124,8 @@ public class AccountDocumentHandler } @Override - public String getQProperty(String prop) { + public String getQProperty( + String prop) { return null; } } diff --git a/services/account/jaxb/src/main/resources/META-INF/persistence.xml b/services/account/service/src/main/resources/META-INF/persistence.xml similarity index 95% rename from services/account/jaxb/src/main/resources/META-INF/persistence.xml rename to services/account/service/src/main/resources/META-INF/persistence.xml index 74090142f..de4e3b2b1 100644 --- a/services/account/jaxb/src/main/resources/META-INF/persistence.xml +++ b/services/account/service/src/main/resources/META-INF/persistence.xml @@ -3,7 +3,7 @@ http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:orm="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> org.hibernate.ejb.HibernatePersistence - cspaceds + CSpaceDS org.collectionspace.services.account.AccountsCommon org.collectionspace.services.account.AccountsCommonList org.collectionspace.services.account.AccountsCommonList$AccountListItem diff --git a/services/common/build.xml b/services/common/build.xml index c162d1199..c689430ee 100644 --- a/services/common/build.xml +++ b/services/common/build.xml @@ -107,6 +107,20 @@ + + + + + + + + + + + + + diff --git a/services/common/lib/README.txt b/services/common/lib/README.txt new file mode 100644 index 000000000..6df592750 --- /dev/null +++ b/services/common/lib/README.txt @@ -0,0 +1,6 @@ +This lib directory contains binaries required to run the common layer +in JBoss container. Corresponding binaries (if present) in JBoss domain are +either upgraded/replaced with a one-time execution task. + +For jpa upgrade in mercury 0.4, the task is 'ant jpa' executed at +service/common level. diff --git a/services/common/lib/jpa-upgrade/ejb3-persistence-1.0.2.GA.jar b/services/common/lib/jpa-upgrade/ejb3-persistence-1.0.2.GA.jar new file mode 100644 index 000000000..32c22667a Binary files /dev/null and b/services/common/lib/jpa-upgrade/ejb3-persistence-1.0.2.GA.jar differ diff --git a/services/common/lib/jpa-upgrade/hibernate-annotations-3.4.0.GA.jar b/services/common/lib/jpa-upgrade/hibernate-annotations-3.4.0.GA.jar new file mode 100644 index 000000000..24b1583cd Binary files /dev/null and b/services/common/lib/jpa-upgrade/hibernate-annotations-3.4.0.GA.jar differ diff --git a/services/common/lib/jpa-upgrade/hibernate-entitymanager-3.4.0.GA.jar b/services/common/lib/jpa-upgrade/hibernate-entitymanager-3.4.0.GA.jar new file mode 100644 index 000000000..662eab11c Binary files /dev/null and b/services/common/lib/jpa-upgrade/hibernate-entitymanager-3.4.0.GA.jar differ diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java b/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java index 48fd89583..47885dc9c 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/AbstractServiceContext.java @@ -46,33 +46,33 @@ public abstract class AbstractServiceContext implements ServiceContext { final Logger logger = LoggerFactory.getLogger(AbstractServiceContext.class); + Map properties = new HashMap(); Map objectPartMap = new HashMap(); private ServiceBindingType serviceBinding; private TenantBindingType tenantBinding; - - private String overrideDocumentType = null; - + private String overrideDocumentType = null; + public AbstractServiceContext(String serviceName) { TenantBindingConfigReader tReader = ServiceMain.getInstance().getTenantBindingConfigReader(); //TODO: get tenant binding from security context (Subject.g String tenantId = "1"; //hardcoded for movingimages.us tenantBinding = tReader.getTenantBinding(tenantId); - if(tenantBinding == null){ + if (tenantBinding == null) { String msg = "No tenant binding found while processing request for " + serviceName; logger.error(msg); throw new IllegalStateException(msg); } serviceBinding = tReader.getServiceBinding(tenantId, serviceName); - if(serviceBinding == null){ + if (serviceBinding == null) { String msg = "No service binding found while processing request for " + serviceName + " for tenant id=" + getTenantId() + " name=" + getTenantName(); logger.error(msg); throw new IllegalStateException(msg); } - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("tenantId=" + tenantId + " service binding=" + serviceBinding.getName()); } @@ -97,14 +97,14 @@ public abstract class AbstractServiceContext @Override public Map getPartsMetadata() { - if(objectPartMap.size() != 0){ + if (objectPartMap.size() != 0) { return objectPartMap; } ServiceBindingType serviceBinding = getServiceBinding(); List objectTypes = serviceBinding.getObject(); - for(ServiceObjectType objectType : objectTypes){ + for (ServiceObjectType objectType : objectTypes) { List objectPartTypes = objectType.getPart(); - for(ObjectPartType objectPartType : objectPartTypes){ + for (ObjectPartType objectPartType : objectPartTypes) { objectPartMap.put(objectPartType.getLabel(), objectPartType); } } @@ -152,19 +152,18 @@ public abstract class AbstractServiceContext @Override public String getServiceName() { return serviceBinding.getName(); - } - + } + @Override public String getDocumentType() { - // If they have not overridden the setting, use the type of the service - // object. - return(overrideDocumentType!=null)?overrideDocumentType: - serviceBinding.getObject().get(0).getName(); + // If they have not overridden the setting, use the type of the service + // object. + return (overrideDocumentType != null) ? overrideDocumentType : serviceBinding.getObject().get(0).getName(); } @Override public void setDocumentType(String docType) { - overrideDocumentType = docType; + overrideDocumentType = docType; } @Override @@ -189,15 +188,38 @@ public abstract class AbstractServiceContext @Override public abstract void setOutput(OT output); + @Override + public Map getProperties() { + return properties; + } + + @Override + public void setProperties(Map props) { + properties.putAll(props); + } + + public Object getProperty(String name) { + return properties.get(name); + } + + public void setProperty(String name, Object o) { + properties.put(name, o); + } + @Override public String toString() { - return "AbstractServiceContext [" + - "service name=" + serviceBinding.getName() + " " + - "service version=" + serviceBinding.getVersion() + " " + - "tenant id=" + tenantBinding.getId() + " " + - "tenant name=" + tenantBinding.getName() + " " + - tenantBinding.getDisplayName() + " " + - "tenant repository domain=" + tenantBinding.getRepositoryDomain() + " " + - "]"; + StringBuilder msg = new StringBuilder(); + msg.append("AbstractServiceContext ["); + msg.append("service name=" + serviceBinding.getName() + " "); + msg.append("service version=" + serviceBinding.getVersion() + " "); + msg.append("tenant id=" + tenantBinding.getId() + " "); + msg.append("tenant name=" + tenantBinding.getName() + " "); + msg.append(tenantBinding.getDisplayName() + " "); + msg.append("tenant repository domain=" + tenantBinding.getRepositoryDomain()); + for(Map.Entry entry : properties.entrySet()) { + msg.append("property name=" + entry.getKey() + " value=" + entry.getValue().toString()); + } + msg.append("]"); + return msg.toString(); } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java index c3723c06d..b3e8e3adc 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java +++ b/services/common/src/main/java/org/collectionspace/services/common/context/ServiceContext.java @@ -23,7 +23,9 @@ */ package org.collectionspace.services.common.context; +import java.util.HashMap; import java.util.Map; +import java.util.Properties; import org.collectionspace.services.common.ClientType; import org.collectionspace.services.common.service.ObjectPartType; import org.collectionspace.services.common.service.ServiceBindingType; @@ -167,6 +169,31 @@ public interface ServiceContext { * @return label */ public String getCommonPartLabel(String schemaName); + + /** + * getProperties retruns user-defined properties associated with this context + * @return + */ + public Map getProperties(); + + + /** + * setProperties sets user-defined properties to this context + * @param props + */ + public void setProperties(Map props); + + + /** + * getProperty returns specified user-defined property + */ + public Object getProperty(String name); + + /** + * setProperty sets user-defined property with given name + */ + public void setProperty(String name, Object o); + } diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java index b5e6cb244..4a8ef9f51 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; +import javax.persistence.NoResultException; import javax.persistence.Persistence; import javax.persistence.Query; import org.collectionspace.services.common.context.ServiceContext; @@ -69,18 +70,18 @@ public class JpaStorageClient implements StorageClient { EntityManager em = null; try { handler.prepare(Action.CREATE); - Object po = handler.getCommonPart(); - DocumentWrapper wrapDoc = new DocumentWrapperImpl(po); + Object entity = handler.getCommonPart(); + DocumentWrapper wrapDoc = new DocumentWrapperImpl(entity); handler.handle(Action.CREATE, wrapDoc); emf = getEntityManagerFactory(docType); em = emf.createEntityManager(); em.getTransaction().begin(); - em.persist(po); + em.persist(entity); em.getTransaction().commit(); handler.complete(Action.CREATE, wrapDoc); - return getCsid(po); + return getCsid(entity); } catch (Exception e) { - if (em != null) { + if (em != null && em.getTransaction().isActive()) { em.getTransaction().rollback(); } if (logger.isDebugEnabled()) { @@ -116,8 +117,9 @@ public class JpaStorageClient implements StorageClient { EntityManager em = null; try { handler.prepare(Action.GET); - StringBuilder queryStr = new StringBuilder("SELECT * FROM "); - queryStr.append(docType); + StringBuilder queryStr = new StringBuilder("SELECT a FROM "); + queryStr.append(getEntityName(ctx)); + queryStr.append(" a"); queryStr.append(" WHERE csid = :csid"); //TODO: add tenant id String where = docFilter.getWhereClause(); @@ -134,10 +136,21 @@ public class JpaStorageClient implements StorageClient { if ((docFilter.getOffset() > 0) || (docFilter.getPageSize() > 0)) { } else { } - //require transaction for get? - em.getTransaction().begin(); - Object o = q.getSingleResult(); - em.getTransaction().commit(); + Object o = null; + + try { + //require transaction for get? + em.getTransaction().begin(); + o = q.getSingleResult(); + em.getTransaction().commit(); + } catch (NoResultException nre) { + if (em != null && em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + String msg = "could not find entity with id=" + id; + logger.error(msg, nre); + throw new DocumentNotFoundException(msg, nre); + } DocumentWrapper wrapDoc = new DocumentWrapperImpl(o); handler.handle(Action.GET, wrapDoc); handler.complete(Action.GET, wrapDoc); @@ -193,8 +206,9 @@ public class JpaStorageClient implements StorageClient { try { handler.prepare(Action.GET_ALL); - StringBuilder queryStr = new StringBuilder("SELECT * FROM "); - queryStr.append(docType); + StringBuilder queryStr = new StringBuilder("SELECT a FROM "); + queryStr.append(getEntityName(ctx)); + queryStr.append(" a"); //TODO: add tenant id String where = docFilter.getWhereClause(); if ((null != where) && (where.length() > 0)) { @@ -246,14 +260,23 @@ public class JpaStorageClient implements StorageClient { EntityManager em = null; try { handler.prepare(Action.UPDATE); - Object po = handler.getCommonPart(); - setCsid(po, id); - DocumentWrapper wrapDoc = new DocumentWrapperImpl(po); + Object entity = handler.getCommonPart(); + setCsid(entity, id); + DocumentWrapper wrapDoc = new DocumentWrapperImpl(entity); handler.handle(Action.UPDATE, wrapDoc); emf = getEntityManagerFactory(docType); em = emf.createEntityManager(); em.getTransaction().begin(); - em.merge(po); + Object entityFound = em.find(entity.getClass(), id); + if(entityFound == null) { + if (em != null && em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + String msg = "could not find entity with id=" + id; + logger.error(msg); + throw new DocumentNotFoundException(msg); + } + em.merge(entity); em.getTransaction().commit(); handler.complete(Action.UPDATE, wrapDoc); } catch (DocumentException de) { @@ -264,6 +287,9 @@ public class JpaStorageClient implements StorageClient { } throw new DocumentException(e); } finally { + if (emf != null) { + releaseEntityManagerFactory(emf); + } } } @@ -275,11 +301,12 @@ public class JpaStorageClient implements StorageClient { * @throws DocumentException */ @Override - public void delete(ServiceContext ctx, String id) throws DocumentNotFoundException, + public void delete(ServiceContext ctx, String id) + throws DocumentNotFoundException, DocumentException { if (logger.isDebugEnabled()) { - logger.debug("deleting document with id=" + id); + logger.debug("deleting entity with id=" + id); } String docType = ctx.getDocumentType(); if (docType == null) { @@ -290,7 +317,7 @@ public class JpaStorageClient implements StorageClient { EntityManager em = null; try { StringBuilder deleteStr = new StringBuilder("DELETE FROM "); - deleteStr.append(docType); + deleteStr.append(getEntityName(ctx)); deleteStr.append(" WHERE csid = :csid"); //TODO: add tenant id @@ -299,23 +326,26 @@ public class JpaStorageClient implements StorageClient { Query q = em.createQuery(deleteStr.toString()); q.setParameter("csid", id); //TODO: add tenant id + int rcount = 0; em.getTransaction().begin(); - int rcount = q.executeUpdate(); - em.getTransaction().commit(); + rcount = q.executeUpdate(); if (rcount != 1) { - throw new DocumentException("failed to delete " + docType + - " csid=" + id); + if (em != null && em.getTransaction().isActive()) { + em.getTransaction().rollback(); + } + String msg = "could not find entity with id=" + id; + logger.error(msg); + throw new DocumentNotFoundException(msg); } + em.getTransaction().commit(); + } catch (DocumentException de) { - if (em != null) { - em.getTransaction().rollback(); - } throw de; } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("Caught exception ", e); } - if (em != null) { + if (em != null && em.getTransaction().isActive()) { em.getTransaction().rollback(); } throw new DocumentException(e); @@ -326,7 +356,8 @@ public class JpaStorageClient implements StorageClient { } } - private EntityManagerFactory getEntityManagerFactory(String persistenceUnit) { + private EntityManagerFactory getEntityManagerFactory( + String persistenceUnit) { return Persistence.createEntityManagerFactory(persistenceUnit); } @@ -335,21 +366,24 @@ public class JpaStorageClient implements StorageClient { if (emf != null) { emf.close(); } + } private String getCsid(Object o) throws Exception { Class c = o.getClass(); Method m = c.getMethod("getCsid"); if (m == null) { - String msg = "Could not find csid in object of class=" + o.getClass().getCanonicalName(); + String msg = "Could not find csid in entity of class=" + o.getClass().getCanonicalName(); logger.error(msg); throw new IllegalArgumentException(msg); } + Object r = m.invoke(o); if (logger.isDebugEnabled()) { - logger.debug("getCsid returned csid=" + r.toString() + + logger.debug("getCsid returned csid=" + r + " for " + c.getName()); } + return (String) r; } @@ -365,19 +399,32 @@ public class JpaStorageClient implements StorageClient { //no need to set return; } + } //set csid Class c = o.getClass(); - Method m = c.getMethod("setCsid"); + Method m = c.getMethod("setCsid", java.lang.String.class); if (m == null) { - String msg = "Could not find csid in object of class=" + o.getClass().getCanonicalName(); + String msg = "Could not find csid in entity of class=" + o.getClass().getCanonicalName(); logger.error(msg); throw new IllegalArgumentException(msg); } + Object r = m.invoke(o, csid); if (logger.isDebugEnabled()) { - logger.debug("setCsid returned csid=" + r.toString() + + logger.debug("completed setCsid " + " for " + c.getName()); } + + } + + private String getEntityName(ServiceContext ctx) { + Object o = ctx.getProperty("entity-name"); + if (o == null) { + throw new IllegalArgumentException("property entity-name missing in context " + + ctx.toString()); + } + + return (String) o; } -} \ No newline at end of file +} diff --git a/services/pom.xml b/services/pom.xml index 9f0c53a19..a77fb66e5 100644 --- a/services/pom.xml +++ b/services/pom.xml @@ -16,7 +16,7 @@ common authentication - + account relation acquisition diff --git a/src/main/resources/config/cspace-ds.xml b/src/main/resources/config/cspace-ds.xml index 8aab83bee..d38540337 100644 --- a/src/main/resources/config/cspace-ds.xml +++ b/src/main/resources/config/cspace-ds.xml @@ -8,6 +8,7 @@ http://www.mysql.com/downloads/api-jdbc-stable.html CspaceDS + false jdbc:mysql://localhost:3306/cspace com.mysql.jdbc.Driver test