emf = JpaStorageUtils.getEntityManagerFactory();
em = emf.createEntityManager();
em.getTransaction().begin();
- Object entityFound = em.find(entityReceived.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);
- }
+ Object entityFound = getEntity(em, id, entityReceived.getClass());
DocumentWrapper<Object> wrapDoc = new DocumentWrapperImpl<Object>(entityFound);
handler.handle(Action.UPDATE, wrapDoc);
em.getTransaction().commit();
}
}
- /* delete use delete to remove parent entityReceived along with child entities
+ /*
+ * delete removes entity and its child entities
+ * cost: a get before delete
* @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String)
*/
@Override
throws DocumentNotFoundException,
DocumentException {
+ if (logger.isDebugEnabled()) {
+ logger.debug("deleting entity with id=" + id);
+ }
+
if (ctx == null) {
throw new IllegalArgumentException(
"JpaStorageClient.delete: ctx is missing");
}
-
- if (logger.isDebugEnabled()) {
- logger.debug("deleting entity with id=" + id);
- }
EntityManagerFactory emf = null;
EntityManager em = null;
try {
* deleteWhere uses the where clause to delete an entityReceived represented by the csidReceived
* it does not delete any child entities.
* @param ctx
- * @param csidReceived
+ * @param id
* @throws DocumentNotFoundException
* @throws DocumentException
*/
}
}
+ @Override
+ public void delete(ServiceContext ctx, String id, DocumentHandler handler)
+ throws DocumentNotFoundException, DocumentException {
+ if (ctx == null) {
+ throw new IllegalArgumentException(
+ "JpaStorageClient.delete: ctx is missing");
+ }
+ if (handler == null) {
+ throw new IllegalArgumentException(
+ "JpaStorageClient.delete: handler is missing");
+ }
+ EntityManagerFactory emf = null;
+ EntityManager em = null;
+ try {
+ handler.prepare(Action.DELETE);
+ Object entity = handler.getCommonPart();
+ DocumentWrapper<Object> wrapDoc = new DocumentWrapperImpl<Object>(entity);
+
+ handler.handle(Action.DELETE, wrapDoc);
+ handler.complete(Action.DELETE, wrapDoc);
+ } catch (DocumentException de) {
+ throw de;
+ } catch (Exception e) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Caught exception ", e);
+ }
+ if (em != null && em.getTransaction().isActive()) {
+ em.getTransaction().rollback();
+ }
+ throw new DocumentException(e);
+ } finally {
+ if (emf != null) {
+ JpaStorageUtils.releaseEntityManagerFactory(emf);
+ }
+ }
+ }
+
/**
* Gets the entityReceived name.
*
protected String getEntityName(ServiceContext ctx) {
Object o = ctx.getProperty(ServiceContextProperties.ENTITY_NAME);
if (o == null) {
- throw new IllegalArgumentException(ServiceContextProperties.ENTITY_NAME +
- "property is missing in context "
+ throw new IllegalArgumentException(ServiceContextProperties.ENTITY_NAME
+ + "property is missing in context "
+ ctx.toString());
}
/**
* getEntity returns persistent entity for given id. it assumes that
- * JpaStorageClientImpl is implemented using the JpaStorageClientImpl(entityClazz)
- * constructor
+ * service context has property ServiceContextProperties.ENTITY_CLASS set
* @param ctx service context
* @param em entity manager
* @param csid received
* @return
- * @throws DocumentNotFoundException
- * @throws UnsupportedOperationException if JpaStorageClientImpl is not implemented
- * using the JpaStorageClientImpl(entityClazz)
- * constructor
+ * @throws DocumentNotFoundException and rollsback the transaction if active
*/
- protected Object getEntity(ServiceContext ctx, EntityManager em, String id) throws DocumentNotFoundException {
+ protected Object getEntity(ServiceContext ctx, EntityManager em, String id)
+ throws DocumentNotFoundException {
Class entityClazz = (Class) ctx.getProperty(ServiceContextProperties.ENTITY_CLASS);
if (entityClazz == null) {
- String msg = ServiceContextProperties.ENTITY_CLASS +
- " property is missing in the context";
+ String msg = ServiceContextProperties.ENTITY_CLASS
+ + " property is missing in the context";
logger.error(msg);
throw new IllegalArgumentException(msg);
}
+ return getEntity(em, id, entityClazz);
+ }
+
+ /**
+ * getEntity retrieves the persistent entity of given class for given id
+ * @param em
+ * @param id entity id
+ * @param entityClazz
+ * @return
+ * @throws DocumentNotFoundException and rollsback the transaction if active
+ */
+ protected Object getEntity(EntityManager em, String id, Class entityClazz)
+ throws DocumentNotFoundException {
Object entityFound = JpaStorageUtils.getEntity(em, id, entityClazz);
if (entityFound == null) {
if (em != null && em.getTransaction().isActive()) {
}
}
-
+
/**
* get document from the Nuxeo repository
* @param ctx service context under which this method is invoked
DocumentFilter docFilter = handler.getDocumentFilter();
if (docFilter == null) {
throw new IllegalArgumentException(
- "RepositoryJavaClient.get: handler has no Filter specified");
+ "RepositoryJavaClient.get: handler has no Filter specified");
}
- if(docFilter.getPageSize()!= 1) {
+ if (docFilter.getPageSize() != 1) {
logger.warn("RepositoryJavaClient.get: forcing docFilter pagesize to 1.");
}
String docType = ctx.getDocumentType();
if (docType == null) {
throw new DocumentNotFoundException(
- "Unable to find DocumentType for service " + ctx.getServiceName());
+ "Unable to find DocumentType for service " + ctx.getServiceName());
}
String domain = ctx.getRepositoryDomainName();
if (domain == null) {
try {
handler.prepare(Action.GET);
repoSession = getRepositorySession();
-
+
DocumentModelList docList = null;
// force limit to 1, and ignore totalSize
- String query = buildNXQLQuery(docType, docFilter.getWhereClause(), domain );
- docList = repoSession.query( query, null, 1, 0, false);
- if(docList.size()!=1) {
+ String query = buildNXQLQuery(docType, docFilter.getWhereClause(), domain);
+ docList = repoSession.query(query, null, 1, 0, false);
+ if (docList.size() != 1) {
throw new DocumentNotFoundException("No document found matching filter params.");
}
DocumentModel doc = docList.get(0);
-
+
if (logger.isDebugEnabled()) {
logger.debug("Executed NXQL query: " + query);
}
-
+
//set reposession to handle the document
((DocumentModelHandler) handler).setRepositorySession(repoSession);
DocumentWrapper<DocumentModel> wrapDoc = new DocumentWrapperImpl<DocumentModel>(doc);
*/
@Override
public DocumentWrapper<DocumentModel> getDoc(
- ServiceContext ctx, String id)
+ ServiceContext ctx, String id)
throws DocumentNotFoundException, DocumentException {
RepositoryInstance repoSession = null;
DocumentWrapper<DocumentModel> wrapDoc = null;
*/
@Override
public DocumentWrapper<DocumentModel> findDoc(
- ServiceContext ctx, String where)
+ ServiceContext ctx, String where)
throws DocumentNotFoundException, DocumentException {
RepositoryInstance repoSession = null;
DocumentWrapper<DocumentModel> wrapDoc = null;
try {
- String docType = ctx.getDocumentType();
+ String docType = ctx.getDocumentType();
if (docType == null) {
throw new DocumentNotFoundException(
- "Unable to find DocumentType for service " + ctx.getServiceName());
+ "Unable to find DocumentType for service " + ctx.getServiceName());
}
String domain = ctx.getRepositoryDomainName();
if (domain == null) {
repoSession = getRepositorySession();
DocumentModelList docList = null;
// force limit to 1, and ignore totalSize
- String query = buildNXQLQuery(docType, where, domain );
- docList = repoSession.query( query, null, 1, 0, false);
- if(docList.size()!=1) {
+ String query = buildNXQLQuery(docType, where, domain);
+ docList = repoSession.query(query, null, 1, 0, false);
+ if (docList.size() != 1) {
if (logger.isDebugEnabled()) {
- logger.debug("findDoc: Query found: "+docList.size()+" items.");
+ logger.debug("findDoc: Query found: " + docList.size() + " items.");
logger.debug(" Query: " + query);
}
throw new DocumentNotFoundException("No document found matching filter params.");
}
return wrapDoc;
}
-
+
/**
* find doc and return CSID from the Nuxeo repository
* @param ctx service context under which this method is invoked
* @throws DocumentException
*/
public String findDocCSID(
- ServiceContext ctx, String where)
+ ServiceContext ctx, String where)
throws DocumentNotFoundException, DocumentException {
- String csid = null;
- try {
- DocumentWrapper<DocumentModel> wrapDoc = findDoc(ctx, where);
- DocumentModel docModel = wrapDoc.getWrappedObject();
+ String csid = null;
+ try {
+ DocumentWrapper<DocumentModel> wrapDoc = findDoc(ctx, where);
+ DocumentModel docModel = wrapDoc.getWrappedObject();
csid = NuxeoUtils.extractId(docModel.getPathAsString());
- } catch (DocumentNotFoundException dnfe) {
+ } catch (DocumentNotFoundException dnfe) {
throw dnfe;
} catch (IllegalArgumentException iae) {
throw iae;
} catch (DocumentException de) {
throw de;
- } catch (Exception e) {
+ } catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Caught exception ", e);
}
throw new DocumentException(e);
}
- return csid;
+ return csid;
}
/**
*/
@Override
public DocumentWrapper<DocumentModelList> findDocs(
- List<String> docTypes, String where, String domain,
- int pageSize, int pageNum, boolean computeTotal )
+ List<String> docTypes, String where, String domain,
+ int pageSize, int pageNum, boolean computeTotal)
throws DocumentNotFoundException, DocumentException {
RepositoryInstance repoSession = null;
DocumentWrapper<DocumentModelList> wrapDoc = null;
try {
- if (docTypes == null || docTypes.size()<1) {
+ if (docTypes == null || docTypes.size() < 1) {
throw new DocumentNotFoundException(
- "findDocs must specify at least one DocumentType.");
+ "findDocs must specify at least one DocumentType.");
}
if (domain == null) {
throw new DocumentNotFoundException("findDocs must specify Domain.");
repoSession = getRepositorySession();
DocumentModelList docList = null;
// force limit to 1, and ignore totalSize
- String query = buildNXQLQuery(docTypes, where, domain );
- docList = repoSession.query( query, null, pageSize, pageNum, computeTotal);
+ String query = buildNXQLQuery(docTypes, where, domain);
+ docList = repoSession.query(query, null, pageSize, pageNum, computeTotal);
wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
} catch (IllegalArgumentException iae) {
throw iae;
return wrapDoc;
}
-
@Override
public void get(ServiceContext ctx, List<String> csidList, DocumentHandler handler)
- throws DocumentNotFoundException, DocumentException {
+ throws DocumentNotFoundException, DocumentException {
if (handler == null) {
throw new IllegalArgumentException(
"RepositoryJavaClient.getAll: handler is missing");
}
}
}
-
+
/**
* getAll get all documents for an entity entity service from the Nuxeo
* repository
handler.prepare(Action.GET_ALL);
repoSession = getRepositorySession();
DocumentModelList docList = null;
- String query = buildNXQLQuery(docType, docFilter.getWhereClause(), domain );
+ String query = buildNXQLQuery(docType, docFilter.getWhereClause(), domain);
// If we have limit and/or offset, then pass true to get totalSize
// in returned DocumentModelList.
} else {
docList = repoSession.query(query);
}
-
+
if (logger.isDebugEnabled()) {
logger.debug("Executed NXQL query: " + query.toString());
}
-
+
//set repoSession to handle the document
((DocumentModelHandler) handler).setRepositorySession(repoSession);
DocumentWrapper<DocumentModelList> wrapDoc = new DocumentWrapperImpl<DocumentModelList>(docList);
}
}
+ @Override
+ public void delete(ServiceContext ctx, String id, DocumentHandler handler)
+ throws DocumentNotFoundException, DocumentException {
+ throw new UnsupportedOperationException();
+ }
+
@Override
public String createWorkspace(String tenantDomain, String workspaceName) throws Exception {
RepositoryInstance repoSession = null;
}
return workspaceId;
}
-
- private final void appendNXQLWhere(StringBuilder query, String where, String domain ) {
+
+ private final void appendNXQLWhere(StringBuilder query, String where, String domain) {
// TODO This is a slow method for tenant-filter
// We should make this a property that is indexed.
query.append(" WHERE ecm:path STARTSWITH '/" + domain + "'");
if ((null != where) && (where.length() > 0)) {
- // Due to an apparent bug/issue in how Nuxeo translates the NXQL query string
- // into SQL, we need to parenthesize our 'where' clause
- query.append(" AND " + "(" + where +")");
+ // Due to an apparent bug/issue in how Nuxeo translates the NXQL query string
+ // into SQL, we need to parenthesize our 'where' clause
+ query.append(" AND " + "(" + where + ")");
}
query.append(" AND ecm:isProxy = 0");
}
- private final String buildNXQLQuery(String docType, String where, String domain ) {
+ private final String buildNXQLQuery(String docType, String where, String domain) {
StringBuilder query = new StringBuilder("SELECT * FROM ");
query.append(docType);
- appendNXQLWhere(query, where, domain );
+ appendNXQLWhere(query, where, domain);
return query.toString();
}
- private final String buildNXQLQuery(List<String> docTypes, String where, String domain ) {
+ private final String buildNXQLQuery(List<String> docTypes, String where, String domain) {
StringBuilder query = new StringBuilder("SELECT * FROM ");
boolean fFirst = true;
- for(String docType:docTypes) {
- if(fFirst) {
+ for (String docType : docTypes) {
+ if (fFirst) {
fFirst = false;
- } else {
- query.append(",");
- }
+ } else {
+ query.append(",");
+ }
query.append(docType);
}
- appendNXQLWhere(query, where, domain );
+ appendNXQLWhere(query, where, domain);
return query.toString();
}