use of com.twinsoft.convertigo.engine.providers.couchdb.CouchClient in project convertigo by convertigo.
the class PurgeDatabaseTransaction method invoke.
@Override
protected JSONObject invoke() throws Exception {
String db = getTargetDatabase();
JSONObject jsonBase;
try {
jsonBase = new JSONObject(getParameterStringValue(CouchParam.json_base));
} catch (Throwable t) {
jsonBase = new JSONObject();
}
JSONObject jsonDocument = jsonBase;
JSONObject response = null;
int length;
int purged = 0;
CouchClient couchClient = getCouchClient();
String version = couchClient.getServerVersion();
if (version.startsWith("2.0.") || version.startsWith("2.1.") || version.startsWith("2.2.")) {
return new JSONObject("{\"error\": \"'_purge' is implemented since CouchDB 2.3.0 and you are using CouchDB " + version + "\"}");
}
boolean old = version != null && version.compareTo("2.") < 0;
if (isPurgeAll()) {
JSONObject body = null;
Map<String, String> query = new HashMap<>(3);
if (!old) {
query.put("filter", "_selector");
body = new JSONObject("{\"selector\":{\"_deleted\":true}}");
}
int limit = 50;
query.put("limit", Integer.toString(limit));
String since = "0";
while (since != null) {
query.put("since", since);
jsonDocument = new JSONObject();
JSONObject json = old ? couchClient.getChanges(db, query) : couchClient.postChanges(db, query, body);
JSONArray changes = CouchKey.results.JSONArray(json);
length = changes.length();
since = length < limit ? null : CouchKey.last_seq.String(json);
handleChanges(changes, old, jsonDocument);
if (jsonDocument.length() > 0) {
response = couchClient.postPurge(db, jsonDocument);
if (CouchKey.purged.has(response)) {
purged += CouchKey.purged.JSONObject(response).length();
} else {
return response;
}
}
}
} else {
Object p = getParameterValue("_id");
List<String> ids = null;
if (p != null) {
if (p instanceof String) {
ids = Arrays.asList((String) p);
} else if (p instanceof List) {
ids = GenericUtils.cast(p);
}
}
if (ids != null && !ids.isEmpty()) {
List<String> revs = null;
if ((p = getParameterValue("_rev")) != null) {
if (p instanceof String) {
revs = Arrays.asList((String) p);
} else if (p instanceof List) {
revs = GenericUtils.cast(p);
}
}
Iterator<String> irev = revs != null ? revs.iterator() : Collections.emptyIterator();
for (String id : ids) {
JSONArray r = null;
if (jsonDocument.has(id)) {
Object o = jsonDocument.get(id);
if (o instanceof JSONArray) {
r = (JSONArray) o;
}
}
if (r == null) {
jsonDocument.put(id, r = new JSONArray());
}
if (irev.hasNext()) {
r.put(irev.next());
}
}
}
if ((length = jsonDocument.length()) > 0) {
Set<String> keys = new HashSet<>(length);
for (Iterator<?> i = jsonDocument.keys(); i.hasNext(); ) {
String key = (String) i.next();
Object o = jsonDocument.get(key);
if (!(o instanceof JSONArray)) {
jsonDocument.put(key, new JSONArray());
keys.add(key);
} else if (((JSONArray) o).length() == 0) {
keys.add(key);
}
}
if (!keys.isEmpty()) {
JSONObject body = new JSONObject();
CouchKey.doc_ids.put(body, new JSONArray(keys));
JSONObject changes = couchClient.postChanges(db, null, body);
JSONArray result = CouchKey.results.JSONArray(changes);
handleChanges(result, false, jsonDocument);
}
if (jsonDocument.length() > 0) {
response = couchClient.postPurge(db, jsonDocument);
purged += CouchKey.purged.JSONObject(response).length();
}
}
}
if (response == null) {
response = new JSONObject("{\"purge_seq\":0,\"purged\":{},\"_c8oMeta\":{\"statusCode\":200,\"status\":\"success\",\"reasonPhrase\":\"OK\",\"headers\":{\"Content-Type\":\"application\\/json\"}}}");
}
CouchKey.total_purged.put(response, purged);
if (purged > 0) {
try {
JSONObject ddoc = couchClient.getDocument(db, DesignDocumentC8o.getId());
JSONObject meta = CouchKey._c8oMeta.JSONObject(ddoc);
if (meta.getInt("statusCode") == 200) {
String dbVersion = Long.toString(System.currentTimeMillis(), Character.MAX_RADIX);
CouchKey.c8oDbVersion.put(ddoc, dbVersion);
couchClient.postDocument(db, ddoc, null, CouchPostDocumentPolicy.none, false);
Engine.logBeans.info("(PurgeDatabaseTransaction) Database '" + db + "' version changed to '" + dbVersion + "'.");
}
} catch (Exception e) {
Engine.logBeans.warn("(PurgeDatabaseTransaction) Failed to update database '" + db + "' version", e);
}
}
return response;
}
use of com.twinsoft.convertigo.engine.providers.couchdb.CouchClient in project convertigo by convertigo.
the class PostSessionTransaction method invoke.
@Override
protected Object invoke() throws Exception {
String name = getParameterStringValue(CouchParam.name);
String password = getParameterStringValue(CouchParam.password);
getConnector().setCouchClient(new CouchClient(getCouchClient().getServerUrl(), name, password));
return getCouchClient().getSession();
}
use of com.twinsoft.convertigo.engine.providers.couchdb.CouchClient in project convertigo by convertigo.
the class CustomTransaction method invoke.
@Override
protected Object invoke() throws Exception {
CouchClient provider = getCouchClient();
String evaluatedUrl = eUrl == null ? "" : eUrl.toString();
URI uri;
if (evaluatedUrl.startsWith("//")) {
uri = new URI(provider.getServerUrl() + evaluatedUrl.substring(1));
} else {
if (!evaluatedUrl.startsWith("/")) {
evaluatedUrl = '/' + evaluatedUrl;
}
String db = getConnector().getDatabaseName();
uri = new URI(provider.getDatabaseUrl(db) + evaluatedUrl);
}
Engine.logBeans.debug("(CustomTransaction) CouchDb request uri: " + uri.toString());
String jsonString = null;
Object jsond = toJson(eData);
if (jsond != null) {
JSONObject jsonData;
if (jsond instanceof JSONObject) {
// comes from a complex variable
jsonData = (JSONObject) jsond;
} else {
jsonData = new JSONObject();
jsonData.put("data", jsond);
}
jsonString = jsonData.toString();
Engine.logBeans.debug("(CustomTransaction) CouchDb request data: " + jsonString);
}
HttpRequestBase request = getHttpVerb().newInstance();
if (request == null) {
throw new EngineException("Unsupported HTTP method");
}
request.setURI(uri);
if (jsonString != null && request instanceof HttpEntityEnclosingRequest) {
provider.setJsonEntity((HttpEntityEnclosingRequest) request, jsonString);
}
return provider.execute(request);
}
Aggregations