@Override public boolean removeDBLease(DBLease lease, String path) { CouchDbClient dbClient = connectionProvider.getDBClient(CouchDbClient.class, path); Response resp = null; if (lease.get_rev() != null) { try { resp = dbClient.remove(lease.get_id(), lease.get_rev()); } catch (Exception e) { DBLease entity2 = reload(lease.get_id(), path); if (entity2 != null) { lease = entity2; resp = dbClient.remove(lease.get_id(), lease.get_rev()); } } } String rev = null; if (resp != null) { rev = resp.getRev(); } else { rev = lease.get_rev(); } dbClient.purge(path, lease.get_id(), new String[] {rev}); return true; }
@Override public DBLease add(DBLease entity, String path) { CouchDbClient dbClient = connectionProvider.getDBClient(CouchDbClient.class, DaoConstants.NOTIFICATION); DBLease found = null; try { found = dbClient.find(DBLeaseImpl.class, entity.get_id()); } catch (Exception e) { } if (found != null) { entity.set_rev(found.get_rev()); } boolean conflict = false; if (entity.get_rev() == null) { Response re = dbClient.save(entity); entity.set_rev(re.getRev()); System.out.println(re); return entity; } else { // entity.set_rev(found.get_rev()); Response re; try { re = dbClient.update(entity); entity.set_rev(re.getRev()); System.out.println(re); } catch (Exception e) { conflict = true; System.out.println(entity); e.printStackTrace(); } } if (conflict && found == null) // update conflict due to previous deleted version { HttpHead head = new HttpHead(dbClient.getDBUri() + entity.get_id()); // HttpResponse response = dbClient.executeRequest(head); // // String revision = response.getFirstHeader("ETAG").getValue(); // //// HttpClientUtils.closeQuietly(response); // closes the response // Params params = null; // found = dbClient.find(DBLeaseImpl.class, entity.get_id(), params); } return entity; // // Map<String, String> options = new HashMap<String, String>(); // options.put(XMLResource.OPTION_ENCODING, BTSConstants.ENCODING); // set // // encoding // // to // // UTF-8 // Resource resource = entity.eResource(); // // check if entity has resource, that is if it was newly created or not // if (resource == null) // { // URI uri = URI.createURI(getLocalDBURL() + "/" + DaoConstants.NOTIFICATION + "/" + // entity.get_id()); // logger.info("Resource was null, object was newly created and is persisted for the first // time: " + uri.path()); // resource = connectionProvider.getEmfResourceSet().createResource(uri); // resource.getContents().add(entity); // } // else // { // CouchDbClient dbClient = connectionProvider.getDBClient( // CouchDbClient.class, DaoConstants.NOTIFICATION); // Response re = dbClient.save(entity); // System.out.println(re); // return entity; // } // while (resource.getContents().size() > 1) // { // resource.getContents().remove(1); // } // // try // { // resource.save(options); // } catch (IOException e) // { // logger.error("error trying to save: " + entity, e); //// throw new RuntimeException("Save Resource failed", e); // } // if (!resource.getContents().isEmpty()) // { // return (DBLease) resource.getContents().remove(0); // } // return entity; }