144 lines
3.6 KiB
Java
144 lines
3.6 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.Matcher;
|
|
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";
|
|
|
|
public static CollectionInfo findCollectionInfo(String colID, List<CollectionInfo>[] collections)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
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 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;
|
|
}
|
|
}
|