aslcore/src/org/gcube/application/framework/core/util/FindInfo.java

221 lines
5.7 KiB
Java

package org.gcube.application.framework.core.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
import org.gcube.application.framework.core.commons.model.CollectionInfo;
import org.gcube.application.framework.core.commons.model.SearchField;
/**
* @author Valia Tsagkalidou (KNUA)
*
*/
public class FindInfo {
public static final String ALL = "ALL";
public static final String NAME = "NAME";
public static final String DESCRIPTION = "DESCRIPTION";
/**
* @param colID
* @param collections
* @return
*/
public static CollectionInfo findCollectionInfo(String colID, List<CollectionInfo>[] collections)
{
if (collections != null) {
for(int counter=0; counter < collections.length;counter++)
{
for(int c2=1; c2 < collections[counter].size(); c2++)
{
if(collections[counter].get(c2).getId().equals(colID))
{
return collections[counter].get(c2);
}
}
}
}
else {
System.out.println("No collections!!");
return null;
}
return null;
}
/**
* @param metadataColID
* @param collections
* @return
*/
public static CollectionInfo findCollectionInfoFromMetadata(String metadataColID, List<CollectionInfo>[] collections)
{
for(int counter=0; counter < collections.length;counter++)
{
for(int c2=1; c2 < collections[counter].size(); c2++)
{
for(int c3=0; c3 < collections[counter].get(c2).getMetadataSize(); c3++)
{
if(collections[counter].get(c2).getMetadataID(c3).equals(metadataColID))
{
return collections[counter].get(c2);
}
}
}
}
return null;
}
/**
* @param term
* @param whereToSearch
* @param collections
* @return
*/
public static List<CollectionInfo> searchCollectionInfo(String term, String whereToSearch, List<CollectionInfo>[] collections)
{
term = term.trim().toLowerCase();
if(term.startsWith("*"))
{
term = term.substring(1);
}
else
{
term = "(\\s|\\p{Punct})" + term;
}
if(term.endsWith("*"))
{
term = term.substring(0,term.length()-1);
}
else
{
term = term + "(\\s|\\p{Punct})";
}
term = term.replaceAll("\\x2A", ".*");
term = term.replaceAll("\\x3F", ".");
Pattern pattern = Pattern.compile(term);
boolean name = false, descr = false;
if(whereToSearch.equals(FindInfo.ALL))
{
name = true;
descr = true;
}
else if(whereToSearch.equals(FindInfo.NAME))
{
name = true;
}
else if(whereToSearch.equals(FindInfo.DESCRIPTION))
{
descr = true;
}
List<CollectionInfo> res = new ArrayList<CollectionInfo>();
for(int counter=0; counter < collections.length;counter++)
{
for(int c2=0; c2 < collections[counter].size(); c2++)
{
if(name)
{
if(pattern.matcher(" " + collections[counter].get(c2).getName().toLowerCase() + " ").find())
{
res.add(collections[counter].get(c2));
continue;
}
}
if(descr)
{
if(pattern.matcher(" " + collections[counter].get(c2).getDescription().toLowerCase() + " ").find())
{
res.add(collections[counter].get(c2));
continue;
}
}
}
}
return res;
}
/**
* @param schemaName
* @param collection
* @return
*/
public static int findCollectionSchema(String schemaName, CollectionInfo collection)
{
for(int counter =0; counter < collection.getMetadataSize(); counter++)
{
if(collection.getSchema(counter).equals(schemaName))
{
return counter;
}
}
return -1;
}
public static int findCollectionSchema(String schemaName, String schemaLanguage, CollectionInfo collection) {
for (int counter = 0; counter < collection.getMetadataSize(); counter++) {
if (collection.getSchema(counter).equals(schemaName))
{
if (collection.getLanguage(counter).equals(schemaLanguage))
return counter;
}
}
return -1;
}
/**
* @param name
* @param schemaName
* @param SchemaHashMap
* @return
*/
public static int findCriterion(String name, String schemaName, HashMap<String, List<SearchField>> SchemaHashMap)
{
List<SearchField> schemaCriteria = SchemaHashMap.get(schemaName);
for(int counter = 0; counter < schemaCriteria.size(); counter++)
{// finding details regarding this criterion
if(schemaCriteria.get(counter).name.equals(name))
{
return counter;
}
}
return -1;
}
/**
* @param collections the available collections in a hierarchical structure
* @return an Array of two hashmaps:
* [0] => contains pairs of <content/metadata collection ID, collection name>
* [1] => contains pairs of <content/metadata collection ID, collection short name>
*/
public static HashMap<String, String>[] getCollectionNames(List<CollectionInfo>[] collections)
{
HashMap<String, String>[] collectionArray = new HashMap[2];
collectionArray[0] = new HashMap<String, String>();
collectionArray[1] = new HashMap<String, String>();
if(collections == null)
return collectionArray;
for(int i=0; i< collections.length; i++)
{
for(int k=0; k < collections[i].size(); k++)
{
for(int j=0; j< collections[i].get(k).getMetadataSize(); j++)
{
collectionArray[0].put(collections[i].get(k).getMetadataID(j), collections[i].get(k).getName());
collectionArray[1].put(collections[i].get(k).getMetadataID(j), collections[i].get(k).getShortName());
}
collectionArray[0].put(collections[i].get(k).getId(), collections[i].get(k).getName());
collectionArray[1].put(collections[i].get(k).getId(), collections[i].get(k).getShortName());
}
}
return collectionArray;
}
}