use of org.wso2.carbon.registry.common.TagCount in project jaggery by wso2.
the class RegistryHostObject method jsFunction_search.
public static Scriptable jsFunction_search(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "search";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof NativeObject)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "json", args[0], false);
}
NativeObject options = (NativeObject) args[0];
CustomSearchParameterBean parameters = new CustomSearchParameterBean();
String path = null;
List<String[]> values = new ArrayList<String[]>();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String val;
for (Object idObj : options.getIds()) {
String id = (String) idObj;
Object value = options.get(id, options);
if (value == null || value instanceof Undefined) {
continue;
}
if ("path".equals(id)) {
path = (String) value;
continue;
}
if ("createdBefore".equals(id) || "createdAfter".equals(id) || "updatedBefore".equals(id) || "updatedAfter".equals(id)) {
long t;
if (value instanceof Number) {
t = ((Number) value).longValue();
} else {
t = Long.parseLong(HostObjectUtil.serializeObject(value));
}
val = new String(dateFormat.format(new Date(t)).getBytes());
} else {
val = HostObjectUtil.serializeObject(value);
}
values.add(new String[] { id, val });
}
parameters.setParameterValues(values.toArray(new String[0][0]));
RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
UserRegistry userRegistry = RegistryHostObjectContext.getRegistryService().getRegistry(registryHostObject.registry.getUserName(), tenantId);
Registry configRegistry = RegistryHostObjectContext.getRegistryService().getConfigSystemRegistry(tenantId);
AdvancedSearchResultsBean resultsBean = registryHostObject.search(configRegistry, userRegistry, parameters);
if (resultsBean.getResourceDataList() == null) {
ScriptableObject error = (ScriptableObject) cx.newObject(thisObj);
error.put("error", error, true);
error.put("description", error, resultsBean.getErrorMessage());
return error;
}
List<ScriptableObject> results = new ArrayList<ScriptableObject>();
for (ResourceData resourceData : resultsBean.getResourceDataList()) {
String resourcePath = resourceData.getResourcePath();
if (path != null && !resourcePath.startsWith(path)) {
continue;
}
ScriptableObject result = (ScriptableObject) cx.newObject(thisObj);
result.put("author", result, resourceData.getAuthorUserName());
result.put("rating", result, resourceData.getAverageRating());
result.put("created", result, resourceData.getCreatedOn().getTime().getTime());
result.put("description", result, resourceData.getDescription());
result.put("name", result, resourceData.getName());
result.put("path", result, resourceData.getResourcePath());
List<ScriptableObject> tags = new ArrayList<ScriptableObject>();
if (resourceData.getTagCounts() != null) {
for (TagCount tagCount : resourceData.getTagCounts()) {
ScriptableObject tag = (ScriptableObject) cx.newObject(thisObj);
tag.put("name", tag, tagCount.getKey());
tag.put("count", tag, tagCount.getValue());
tags.add(tag);
}
}
result.put("tags", result, cx.newArray(thisObj, tags.toArray()));
results.add(result);
}
return cx.newArray(thisObj, results.toArray());
} catch (RegistryException e) {
throw new ScriptException(e);
} catch (CarbonException e) {
throw new ScriptException(e);
}
}
Aggregations