* @return a list of tags filtered by tag name or host name
*/
public java.util.List<Tag> getFilteredTags(String tagName, String hostFilter, boolean globalTagsFilter, String sort, int start, int count) {
try {
HibernateUtil dh = new HibernateUtil(Tag.class);
List list = new java.util.ArrayList();
Host host = null;
try {
host = APILocator.getHostAPI().find(hostFilter, APILocator.getUserAPI().getSystemUser(), true);
}catch (Exception e){
Logger.warn(Tag.class, "Unable to get host according to search criteria - hostId = "+ hostFilter);
}
if (UtilMethods.isSet(host)){
String sortStr = "";
if (UtilMethods.isSet(sort)) {
String sortDirection = sort.startsWith("-")?"desc":"asc";
sort = sort.startsWith("-")?sort.substring(1,sort.length()):sort;
if(sort.equalsIgnoreCase("hostname")) sort = "host_id";
sortStr = " order by " + sort + " " + sortDirection;
} else {
sortStr = "order by tagname";
}
//if tag name and host name are set as filters
//search by name, global tags and current host.
if(UtilMethods.isSet(tagName) && globalTagsFilter){
dh.setQuery("from tag in class com.dotmarketing.tag.model.Tag where lower(tagname) like ? and (host_id = ? or host_id = ?) " + sortStr);
dh.setParam("%" + tagName.toLowerCase() + "%");
try {
dh.setParam(host.getMap().get("tagStorage").toString());
} catch(NullPointerException e) {
dh.setParam(Host.SYSTEM_HOST);
}
dh.setParam(Host.SYSTEM_HOST);
}
//if global host is not set as unique filter but tag name is set, search in current host
else if (UtilMethods.isSet(tagName) && !globalTagsFilter){
dh.setQuery("from tag in class com.dotmarketing.tag.model.Tag where lower(tagname) like ? and (host_id = ?) " + sortStr);
dh.setParam("%" + tagName.toLowerCase() + "%");
try {
dh.setParam(host.getMap().get("tagStorage").toString());
} catch(NullPointerException e) {
dh.setParam(Host.SYSTEM_HOST);
}
}else if(!UtilMethods.isSet(tagName) && globalTagsFilter){
//check if tag name is not set and if should display global tags
//it will check all global tags and current host tags.
dh.setQuery("from tag in class com.dotmarketing.tag.model.Tag where "+
"(host_id = ? or host_id = ? ) " + sortStr);
try {
dh.setParam(host.getMap().get("tagStorage").toString());
} catch(NullPointerException e) {
dh.setParam(Host.SYSTEM_HOST);
}
dh.setParam(Host.SYSTEM_HOST);
} else {
//check all current host tags.
String sql = "from tag in class com.dotmarketing.tag.model.Tag ";
Object tagStorage = host.getMap().get("tagStorage");
if(UtilMethods.isSet(tagStorage)){
sql = sql + "where ( host_id = ? ) " + sortStr;
dh.setQuery(sql);
dh.setParam(tagStorage.toString());
}else{
dh.setQuery(sql);
}
}
dh.setFirstResult(start);
list = dh.list();
return list;
}
} catch (Exception e) {
Logger.warn(Tag.class, "getFilteredTags failed:" + e, e);