From: Niels Erik G. Nielsen Date: Fri, 31 May 2013 14:12:55 +0000 (-0400) Subject: Moves UI convenience methods to UiUtils class X-Git-Tag: v0.0.7~69 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=539ba70e5884bb15fe735dc67626c02fce6ad71d;p=mkjsf-moved-to-github.git Moves UI convenience methods to UiUtils class Object exposed to JSF pages as 'pzui' --- diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseDataObject.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseDataObject.java index 572be9a..535f859 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseDataObject.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseDataObject.java @@ -51,38 +51,7 @@ public class ResponseDataObject implements Serializable { public List getElements (String name) { return elements.get(name); } - - public List getElements(String name, int maxElements) { - if (getElements(name)!=null) { - int size = getElements(name).size(); - if (size>0) { - return ((ArrayList) getElements(name)).subList(0, Math.min(maxElements,size)); - } - } - return getElements(name); - } - - public List getElements(String name, int maxElements, int maxTotalValueLength) { - if (getElements(name)!=null) { - int size = getElements(name).size(); - if (size>0) { - List maxElementsList = ((ArrayList) getElements(name)).subList(0, Math.min(maxElements,size)); - int i = 0; - int totalLength = 0; - for (ResponseDataObject element : maxElementsList) { - totalLength += element.getValue().length(); - i++; - if (totalLength>maxTotalValueLength) { - break; - } - } - return maxElementsList.subList(0, i); - } - } - return getElements(name); - } - - + public ResponseDataObject getOneElement (String name) { if (elements.get(name) != null) { return elements.get(name).get(0); diff --git a/src/main/java/com/indexdata/mkjsf/utils/UiUtils.java b/src/main/java/com/indexdata/mkjsf/utils/UiUtils.java new file mode 100644 index 0000000..d6ffe2c --- /dev/null +++ b/src/main/java/com/indexdata/mkjsf/utils/UiUtils.java @@ -0,0 +1,95 @@ +package com.indexdata.mkjsf.utils; + +import java.util.ArrayList; +import java.util.List; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Named; + +import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject; + +@Named("pzui") +@ApplicationScoped +public class UiUtils { + + /** + * Concatenates a list of strings + * @param strings + * @return + */ + public static String concat(String... strings) { + StringBuilder concatenated = new StringBuilder(""); + for (String string : strings) { + concatenated.append(string); + } + return concatenated.toString(); + } + + public static String concatMaxLength(int maxTotalLength, String... strings) { + String concatenated = concat(strings); + return maxLength(concatenated,maxTotalLength); + } + + public static String maxLength (String string, int maxLength) { + if (string == null || string.length()<=maxLength) { + return string; + } else { + return string.substring(0,maxLength); + } + } + + public static String maxLengthMoreLabel (String string, int maxLength, String moreLabel) { + if (string == null || string.length()<=maxLength) { + return string; + } else { + String maxString = maxLength(string,maxLength); + if (string.length()>maxString.length()) { + maxString += moreLabel; + } + return maxString; + } + } + + /** + * Encloses a string with quotation marks + * @param string + * @return + */ + public static String quotes(String string) { + return "\"" + string + "\""; + } + + public static List getMaxNumElements(ResponseDataObject container, String elementName, int maxElements) { + if (container.getElements(elementName)!=null) { + int elementCount = container.getElements(elementName).size(); + if (elementCount>0) { + return ((ArrayList) container.getElements(elementName)).subList(0, Math.min(maxElements,elementCount)); + } + } + return container.getElements(elementName); + } + + public List getMaxElements(ResponseDataObject container, String elementName, int maxElements, int maxTotalValueLength, boolean hardLimit) { + List maxNumList = getMaxNumElements(container,elementName,maxElements); + if (maxNumList!=null) { + int elementCount = maxNumList.size(); + if (elementCount>0) { + int i = 0; + int totalLength = 0; + for (ResponseDataObject element : maxNumList) { + totalLength += element.getValue().length(); + if (totalLength>maxTotalValueLength) { + if (!hardLimit) { + i++; + } + break; + } else { + i++; + } + } + return maxNumList.subList(0, i); + } + } + return maxNumList; + } +} diff --git a/src/main/java/com/indexdata/mkjsf/utils/Utils.java b/src/main/java/com/indexdata/mkjsf/utils/Utils.java index 3d57494..5b8a696 100644 --- a/src/main/java/com/indexdata/mkjsf/utils/Utils.java +++ b/src/main/java/com/indexdata/mkjsf/utils/Utils.java @@ -3,8 +3,6 @@ package com.indexdata.mkjsf.utils; import javax.enterprise.context.ApplicationScoped; import javax.inject.Named; -@Named("utils") -@ApplicationScoped public class Utils { public static String nl = System.getProperty("line.separator"); @@ -27,11 +25,4 @@ public class Utils { } } - public static String concat(String... strings) { - StringBuilder concatenated = new StringBuilder(""); - for (String string : strings) { - concatenated.append(string); - } - return concatenated.toString(); - } }