From 539ba70e5884bb15fe735dc67626c02fce6ad71d Mon Sep 17 00:00:00 2001 From: "Niels Erik G. Nielsen" Date: Fri, 31 May 2013 10:12:55 -0400 Subject: [PATCH] Moves UI convenience methods to UiUtils class Object exposed to JSF pages as 'pzui' --- .../mkjsf/pazpar2/data/ResponseDataObject.java | 33 +------ .../java/com/indexdata/mkjsf/utils/UiUtils.java | 95 ++++++++++++++++++++ src/main/java/com/indexdata/mkjsf/utils/Utils.java | 9 -- 3 files changed, 96 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/indexdata/mkjsf/utils/UiUtils.java 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(); - } } -- 1.7.10.4