1 package com.indexdata.mkjsf.utils;
\r
3 import java.util.ArrayList;
\r
4 import java.util.List;
\r
6 import javax.enterprise.context.ApplicationScoped;
\r
7 import javax.inject.Named;
\r
9 import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject;
\r
14 * @author Niels Erik
\r
19 public class UiUtils {
\r
22 * Concatenates a list of strings
\r
26 public static String concat(String... strings) {
\r
27 StringBuilder concatenated = new StringBuilder("");
\r
28 for (String string : strings) {
\r
29 concatenated.append(string);
\r
31 return concatenated.toString();
\r
34 public static String concatMaxLength(int maxTotalLength, String... strings) {
\r
35 String concatenated = concat(strings);
\r
36 return maxLength(concatenated,maxTotalLength);
\r
39 public static String maxLength (String string, int maxLength) {
\r
40 if (string == null || string.length()<=maxLength) {
\r
43 return string.substring(0,maxLength);
\r
47 public static String maxLengthMoreLabel (String string, int maxLength, String moreLabel) {
\r
48 if (string == null || string.length()<=maxLength) {
\r
51 String maxString = maxLength(string,maxLength);
\r
52 if (string.length()>maxString.length()) {
\r
53 maxString += moreLabel;
\r
60 * Encloses a string with quotation marks
\r
64 public static String quote(String string) {
\r
65 return "\"" + string + "\"";
\r
69 * Gets at most maxElements elements of the given elementName
\r
70 * @param container The parent element containing the elements to retrieve
\r
71 * @param elementName The name of the element(s) to retrieve
\r
72 * @param maxElements Maximum number of elements to retrieve
\r
73 * @return At most maxElements data objects of the given type
\r
75 public static List<ResponseDataObject> getMaxNumElements(ResponseDataObject container, String elementName, int maxElements) {
\r
76 if (container.getElements(elementName)!=null) {
\r
77 int elementCount = container.getElements(elementName).size();
\r
78 if (elementCount>0) {
\r
79 return ((ArrayList<ResponseDataObject>) container.getElements(elementName)).subList(0, Math.min(maxElements,elementCount));
\r
82 return container.getElements(elementName);
\r
86 * Gets at most maxElements data objects, up to a total string length of maxTotalValueLength
\r
87 * @param container The parent element containing the elements to retrieve
\r
88 * @param elementName The name of the element(s) to retrieve
\r
89 * @param maxElements Maximum number of elements to retrieve
\r
90 * @param maxTotalValueLength The maximum total string length of the values of the elements retrieved
\r
91 * @param hardLimit If set to true, the list will be cut of at or below the total string length, if false, the list
\r
92 * will contain the first element that exceeds the length limit - for instance thus guaranteeing
\r
93 * that at least one of the elements will be returned, no matter it's length.
\r
94 * @return Delimited list of elements
\r
96 public List<ResponseDataObject> getMaxElements(ResponseDataObject container, String elementName, int maxElements, int maxTotalValueLength, boolean hardLimit) {
\r
97 List<ResponseDataObject> maxNumList = getMaxNumElements(container,elementName,maxElements);
\r
98 if (maxNumList!=null) {
\r
99 int elementCount = maxNumList.size();
\r
100 if (elementCount>0) {
\r
102 int totalLength = 0;
\r
103 for (ResponseDataObject element : maxNumList) {
\r
104 totalLength += element.getValue().length();
\r
105 if (totalLength>maxTotalValueLength) {
\r
114 return maxNumList.subList(0, i);
\r
117 return maxNumList;
\r