I have a taxonomy index that describes a tree structure. When performing a query I want to get the number of hits for multiple categories (not necessarily in the same level of the tree). For example, given the following list of paths:
[Root/Cat1, Root/Cat1/Cat12, Root/Cat3]
I want to obtain the number of hits for each of these three categories.
I've been looking for a solution and I know that is possible to make a tree request and then get the results by calling .getSubResults() (as it is explained in the API). However I haven't found any example and I don't really know how to implement it. So far I've got to the following:
// Build query
Query query = extendQuery(queryGenerator.generateQuery(resource));
// Set the number of top results
TopScoreDocCollector tdc = TopScoreDocCollector.create(numTopDocuments, true);
// Set a faceted search
FacetSearchParams facetSearchParams = new FacetSearchParams();
// Search at level of the category in interests
CountFacetRequest facetRequest = new CountFacetRequest(new CategoryPath("Top", '/'), numTopCategories);
facetRequest.setResultMode(ResultMode.PER_NODE_IN_TREE);
facetSearchParams.addFacetRequest(facetRequest);
// To collect the number of hits per facet
FacetsCollector facetsCollector =
new FacetsCollector(facetSearchParams, documentReader, taxonomyReader);
try {
// Collect the number of hits per facet
documentSearcher
.search(query, MultiCollector.wrap(tdc, facetsCollector));
for (FacetResult res : facetsCollector.getFacetResults()){
//this is the top lvl facet
FacetResultNode toplvl = res.getFacetResultNode();
System.out.println(toplvl.getLabel() + " (" + toplvl.getValue() + ")");
for (FacetResultNode secondlvl : toplvl.getSubResults()) {
//second lvl facet categories
System.out.println(" " + secondlvl.getLabel().getComponent(1)
+ " (" + secondlvl.getValue() + ")");
for (FacetResultNode thirdlvl : secondlvl.getSubResults()) {
//second lvl facet categories
System.out.println(" " + thirdlvl.getLabel().getComponent(2)
+ " (" + thirdlvl.getValue() + ")");
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However, when I get to the third level I get null. What is going on?
Thanks.
You have to set also:
facetRequest.setDepth(MAX_DEPTH_TREE);
Related
I want to delete many managed objects, selected by fragment type. There are more then 2000 elements in it. Unfortunately I can not delete all with one function call. I have to call this function many times until I have deleted all. How can I delete a list of managed objects in a sufficient way? Not defining page size did not help...
This is my current function:
InventoryFilter filter = new InventoryFilter();
filter.byFragmentType("xy_fragment");
ManagedObjectCollection moc = inventoryApi.getManagedObjectsByFilter(filter);
int count = 0;
// max page size is 2000
for (ManagedObjectRepresentation mo : moc.get(2000).allPages()) {
if (mo.get("c8y_IsBinary") != null) {
binariesApi.deleteFile(mo.getId());
} else {
inventoryApi.delete(mo.getId());
}
LOG.debug(count + " remove: " + mo.getName() + ", " + mo.getType());
count++;
}
LOG.info("all objectes removed, count:" + count);
By calling moc.get(2000).allPages() you already obtain an iterator that queries following pages on demand as you iterate over it.
The problem you are facing is caused by deleting elements from the same list you are iterating over. You delete element from the first page, but once the second page is queried from the server it does not contain the expected elements anymore because you already deleted the first page. Now all elements are shifted forward by your page size.
You can avoid all of that by making a local copy of all elements you want to delete first:
List<ManagedObjectRepresentation> allObjects = Lists.newArrayList( moc.get(2000).allPages())
for (ManagedObjectRepresentation mo : allObjects) {
//delete here
}
There is no bulk delete allowed on the inventory API so your method of looping through the objects is the correct approach.
A bulk delete is already a dangerous tool on the other APIs but on the inventory API it would give you the potential to accidentally delete all your data with just one call (as all data associated with a managedObject is also deleted upon the deletion of the managedObject).
That is why it is not available.
I solved the problem by calling the method until no elements can be found any more. It is not nice but I have no other idea.
public synchronized void removeManagedObjects(String deviceTypeKey) {
int count = 0;
do {
count = deleteManagedObjectes(deviceTypeKey);
}while(count > 0);
}
private int deleteManagedObjectes(String deviceTypeKey) {
InventoryFilter filter = new InventoryFilter();
filter.byFragmentType("xy_fragment");
ManagedObjectCollection moc = inventoryApi.getManagedObjectsByFilter(filter);
int count = 0;
if(moc == null) {
LOG.info("ManagedObjectCollection are NULL");
return count;
}
for (ManagedObjectRepresentation mo : moc.get(2000).allPages()) {
if (mo.get("c8y_IsBinary") != null) {
binariesApi.deleteFile(mo.getId());
} else {
inventoryApi.delete(mo.getId());
}
LOG.debug(count + " remove: " + mo.getName() + ", " + mo.getType());
count++;
}
LOG.info("all objectes removed, count:" + count);
return count;
}
for a project I am currently working on I need to gather tweets from a stream only for one country.
Although the Twitter4j streaming API allows to filter by language the results aren't accurate enough. So I thought to put a filter on top of the filter by checking if the country attribute of the tweet is filled. This works fine when I check if theres a value at all:
if(status.getPlace().getCountry() != null) {
System.out.println("User: " + status.getUser().getName());
System.out.println("Text: : " + status.getText());
System.out.println("Country: " + status.getPlace().getCountry());
System.out.println("Language: " + status.getLang());
}
}
TwitterStream ts = new TwitterStreamFactory(cb.build()).getInstance();
ts.addListener(listener);
FilterQuery filter = new FilterQuery();
String[] language = {"country"};
String[] keywords = {"some keywords"};
filter.track(keywords);
filter.language(language);
ts.filter(filter);
But if I check for a certain country e.g. germany I don't receive any tweets:
if(status.getPlace().getCountry() != "germany") {
System.out.println("User: " + status.getUser().getName());
System.out.println("Text: : " + status.getText());
System.out.println("Country: " + status.getPlace().getCountry());
System.out.println("Language: " + status.getLang());
}
}
It would be great if there's someone who can help me with this.
filter.track(keywords);
filter.language(language);
Keep in mind that the above code means track(keywords) OR language(language).
This is not logical AND.
If you want tweets from only one country, and with certain keywords, then remove filter.language(language) and check the country after you get status.
if(status.getPlace().getCountry().equalsIgnoreCase("germany")) {}
// String comparison
You won't get a tweet from Germany if nobody from Germany tweets with the filters you have specified.
Is there any way to programmatically understand what properties there are for a custom model in Alfresco?
For example, data dictionary in oracle helps you to find what tables are defined by which column names and column data types.
My goal is a sample code in java which extracts all custom defined content models, their attributes, their attributes data types, etc. For example my sample code should return to me that there is a custom content Model which has an integer attribute with name 'No' and a string attribute with name 'Description'. I know this is possible with DictionaryComponent, But I don't know how should I use it.
Take a look at org.alfresco.service.cmr.dictionary.DictionaryService.
will your Java-code running in-context with alfresco (as part of the /alfresco webapp) or outside?
If your running in-context, then Alfreso Java Foundation API is your choice where DictionaryService is part of. just inject the bean into your java code.
If your code is running outside of alfresco, then Alfresco REST API or CMIS is your choice.
If you're still lost, then train yourself first. Jeff wrote some good articles http://ecmarchitect.com/alfresco-developer-series & also a book.
Also a good starting point - Alfresco Developer Docs: http://docs.alfresco.com/4.2/topic/com.alfresco.enterprise.doc/concepts/dev-for-developers.html
thanks a lot of alfresian responses, My code sample is something like below:
I found the sample code from : this URL
public void GetAllAvailableDataTypes() throws IOException
{
Session session = getSession();
boolean includePropertyDefintions = true;
for (Tree t : session.getTypeDescendants(
null, // start at the top of the tree
-1, // infinite depth recursion
includePropertyDefintions // include prop defs
)) {
printTypes(t, "");
}
}
public void printTypes(Tree tree, String tab) {
ObjectType objType = (ObjectType) tree.getItem();
String type = objType.getId();
if(true)//type.endsWith("hstcase"))
{
System.out.println(tab + "TYPE:" + objType.getDisplayName() +
" (" + objType.getDescription() + ")");
//Print some of the common attributes for this type
System.out.print(tab + " Id:" + "-----"+ objType.getId() + "-----");
System.out.print(" Fileable:" + objType.isFileable());
System.out.print(" Queryable:" + objType.isQueryable());
if (objType instanceof DocumentType) {
System.out.print(" [DOC Attrs->] Versionable:" +
((DocumentType)objType).isVersionable());
System.out.println(" Content:" +
((DocumentType)objType).getContentStreamAllowed());
Map<String, org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition<?>> props = objType.getPropertyDefinitions();
Set<String> keys = props.keySet();
Collection<org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition<?>> porpsAsCollection = props.values();
for(org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition<?> p: porpsAsCollection)
{
//System.out.println(p.getDescription());
PropertyType pt = p.getPropertyType();
System.out.println("Display Name: "+ p.getDisplayName());
System.out.println("Local Name: "+ p.getLocalName());
System.out.println("Attribute Type: "+ pt.value());
}
}
System.out.println(""); // end the line
}
List<Tree> childs = tree.getChildren();
for (Tree t : childs) {
// there are more - call self for next level
printTypes(t, tab + " ");
}
}
in websql we can request a certain row like this:
tx.executeSql('SELECT * FROM tblSettings where id = ?', [id], function(tx, rs){
// do stuff with the resultset.
},
function errorHandler(tx, e){
// do something upon error.
console.warn('SQL Error: ', e);
});
however, I know regular SQL and figured i should be able to request
var arr = [1, 2, 3];
tx.executeSql('SELECT * FROM tblSettings where id in (?)', [arr], function(tx, rs){
// do stuff with the resultset.
},
function errorHandler(tx, e){
// do something upon error.
console.warn('SQL Error: ', e);
});
but that gives us no results, the result is always empty. if i would remove the [arr] into arr, then the sql would get a variable amount of parameters, so i figured it should be [arr]. otherwise it would require us to add a dynamic amount of question marks (as many as there are id's in the array).
so can anyone see what i'm doing wrong?
aparently, there is no other solution, than to manually add a question mark for every item in your array.
this is actually in the specs on w3.org
var q = "";
for each (var i in labels)
q += (q == "" ? "" : ", ") + "?";
// later to be used as such:
t.executeSql('SELECT id FROM docs WHERE label IN (' + q + ')', labels, function (t, d) {
// do stuff with result...
});
more info here: http://www.w3.org/TR/webdatabase/#introduction (at the end of the introduction)
however, at the moment i created a helper function that creates such a string for me
might be better than the above, might not, i haven't done any performance testing.
this is what i use now
var createParamString = function(arr){
return _(arr).map(function(){ return "?"; }).join(',');
}
// when called like this:
createparamString([1,2,3,4,5]); // >> returns ?,?,?,?,?
this however makes use of the underscore.js library we have in our project.
Good answer. It was interesting to read an explanation in the official documentation.
I see this question was answered in 2012. I tried it in Google 37 exactly as it is recommened and this is what I got.
Data on input: (I outlined them with the black pencil)
Chrome complains:
So it accepts as many question signs as many input parameters are given. (Let us pay attention that although array is passed it's treated as one parameter)
Eventually I came up to this solution:
var activeItemIds = [1,2,3];
var q = "";
for (var i=0; i< activeItemIds.length; i++) {
q += '"' + activeItemIds[i] + '", ';
}
q= q.substring(0, q.length - 2);
var query = 'SELECT "id" FROM "products" WHERE "id" IN (' + q + ')';
_db.transaction(function (tx) {
tx.executeSql(query, [], function (tx, results1) {
console.log(results1);
debugger;
}, function (a, b) {
console.warn(a);
console.warn(b);
})
})
I have to enforce a policy issuing a warning if items not belonging to a particular category are being added, apart from the three which are allowed and disallowing such additions.....
So far i am able to find the items and issue warning.... but not sure how to stop them from being added....
For Eg.
Allowed categories Shoes and socks
but if i try and add a vegetable item to the inventory it should give me a warning saying "category not allowed../nItem will not be added to inventory"..... and then proceed to the next item....
This is what i've written so far.....
pointcut deliverMessage() :
call(* SC.addItem(..));
pointcut interestingCalls(String category) :
call(Item.new(..)) && args(*, *, category);
before(String category): interestingCalls(category) {
if(category.equals("Socks")) {
System.out.println("category detect: " + category);
else if(category.equals("Shoes"))
System.out.println("category detect: " + category);
else {
check=true;
System.out.println("please check category " + category);
}
}
Why not use the around aspect instead. Then, if they are not of the correct category you don't go into that method, so it gets skipped, if the skipped method is just doing the adding.
UPDATE:
Here is an example from AspectJ In Action, by Manning Publication.
public aspect ProfilingAspect {
pointcut publicOperation() : execution(public * *.*(..));
Object around() : publicOperation() {
long start = System.nanoTime();
Object ret = proceed();
long end = System.nanoTime();
System.out.println(thisJoinPointStaticPart.getSignature()
+ " took " + (end-start) + " nanoseconds");
return ret;
}
}
So, if you wanted to check if you should add the item, if it is an allowed category then just call proceed, otherwise you would just return a null perhaps.