From: Niels Erik G. Nielsen Date: Fri, 31 May 2013 00:40:04 +0000 (-0400) Subject: Adds convenience methods for UI pages X-Git-Tag: v0.0.7~70 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=ea49f38ba51c8325879b693536940974b73a01c2;p=mkjsf-moved-to-github.git Adds convenience methods for UI pages Renames one method and add others for conveniently getting data from response data objects. Adds utility method for concatenating strings in EL expressions (no such built-in function in JSF EL) --- diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java index db4a7bc..4302d9d 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java @@ -37,19 +37,19 @@ public class CommandError extends ResponseDataObject implements ErrorInterface { } public String getLabel() { - return getOneElementValue("commandname"); + return getOneValue("commandname"); } public String getMessage() { if (isServiceError()) { return getServiceError().getMsg(); } else { - return getOneElementValue("errormessage"); + return getOneValue("errormessage"); } } public String getException () { - return getOneElementValue("exception"); + return getOneValue("exception"); } public List getSuggestions() { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Hit.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Hit.java index 6eb291e..7b6885a 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Hit.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Hit.java @@ -20,11 +20,11 @@ public class Hit extends ResponseDataObject { } public String getTitle () { - return getOneElementValue("md-title"); + return getOneValue("md-title"); } public String getTitleRemainder() { - return getOneElementValue("md-title-remainder"); + return getOneValue("md-title-remainder"); } public String getAuthor (String prefix) { @@ -32,24 +32,27 @@ public class Hit extends ResponseDataObject { } public String getAuthor () { - return getOneElementValue("md-author"); + return getOneValue("md-author"); } public String getTitleResponsibility() { - return getOneElementValue("md-title-responsibility"); + return getOneValue("md-title-responsibility"); } public String getDate() { - return getOneElementValue("md-date"); + return getOneValue("md-date"); } public String getTitleComplete() { - return getOneElementValue("md-title-complete"); + return getOneValue("md-title-complete"); } public String getRecId() { - return getOneElementValue("recid"); + return getOneValue("recid"); + } + + public String getDescription() { + return getOneValue("md-description"); } - } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/InitResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/InitResponse.java index 6599082..76eda96 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/InitResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/InitResponse.java @@ -5,7 +5,7 @@ public class InitResponse extends ResponseDataObject { private static final long serialVersionUID = -1479775157276901600L; public String getStatus() { - return getOneElementValue("status"); + return getOneValue("status"); } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Location.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Location.java index b66b717..19b1dcf 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Location.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Location.java @@ -22,7 +22,7 @@ public class Location extends ResponseDataObject { } public String getSubject() { - return getOneElementValue("md-subject"); + return getOneValue("md-subject"); } public void setSequenceNumber(int num) { @@ -47,7 +47,7 @@ public class Location extends ResponseDataObject { } public String getAuthor() { - return getOneElementValue("md-author"); + return getOneValue("md-author"); } public String getAuthors() { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordResponse.java index 89f5fbb..b1de2da 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/RecordResponse.java @@ -11,7 +11,7 @@ public class RecordResponse extends ResponseDataObject { private static final long serialVersionUID = 6682722004285796002L; public String getRecId () { - return getOneElementValue("recid"); + return getOneValue("recid"); } public List getLocations() { @@ -29,19 +29,19 @@ public class RecordResponse extends ResponseDataObject { } public String getTitle() { - return getOneElementValue("md-title"); + return getOneValue("md-title"); } public String getDate() { - return getOneElementValue("md-date"); + return getOneValue("md-date"); } public String getAuthor() { - return getOneElementValue("md-author"); + return getOneValue("md-author"); } public String getSubject() { - return getOneElementValue("md-subject"); + return getOneValue("md-subject"); } public String getSubjects() { @@ -65,7 +65,7 @@ public class RecordResponse extends ResponseDataObject { if (getIsBinary()) { return "0"; } else { - String activeclients = getOneElementValue("activeclients"); + String activeclients = getOneValue("activeclients"); if (activeclients == null || activeclients.length()==0) { // Look for override activeclients = getAttribute("activeclients"); 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 8ffd810..572be9a 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseDataObject.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseDataObject.java @@ -43,7 +43,7 @@ public class ResponseDataObject implements Serializable { elements.get(name).add(value); } else { List list = new ArrayList(); - list.add(value); + list.add(value); elements.put(name,list); } } @@ -52,6 +52,37 @@ public class ResponseDataObject implements Serializable { 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); @@ -66,7 +97,7 @@ public class ResponseDataObject implements Serializable { * @param name of the element * @return text value, empty string if none found */ - public String getOneElementValue (String name) { + public String getOneValue (String name) { if (getOneElement(name)!=null && getOneElement(name).getValue().length()>0) { return getOneElement(name).getValue(); } else { @@ -74,6 +105,19 @@ public class ResponseDataObject implements Serializable { } } + public String[] getValueArray (String name) { + List elements = getElements(name); + String[] valueArray = {}; + if (elements != null) { + valueArray = new String[elements.size()]; + int i = 0; + for (ResponseDataObject element : elements) { + valueArray[i++] = element.getValue(); + } + } + return valueArray; + } + public void appendContent (String content) { textContent = textContent + content; } @@ -92,7 +136,7 @@ public class ResponseDataObject implements Serializable { } public int getIntValue(String name) { - String val = getOneElementValue(name); + String val = getOneValue(name); if (val.length()==0) { return 0; } else { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/SearchResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/SearchResponse.java index 79bb667..76a41b1 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/SearchResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/SearchResponse.java @@ -6,7 +6,7 @@ public class SearchResponse extends ResponseDataObject { private boolean isNew = true; public String getStatus() { - return getOneElementValue("status"); + return getOneValue("status"); } public boolean isNew () { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ShowResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ShowResponse.java index 2bf0a92..c845c3a 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ShowResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ShowResponse.java @@ -12,11 +12,11 @@ public class ShowResponse extends ResponseDataObject { public String getStatus() { - return getOneElementValue("status"); + return getOneValue("status"); } public String getActiveClients () { - return getOneElementValue("activeclients"); + return getOneValue("activeclients"); } public int getMerged () { @@ -24,7 +24,7 @@ public class ShowResponse extends ResponseDataObject { } public String getTotal () { - return getOneElementValue("total"); + return getOneValue("total"); } public int getStart () { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/StatResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/StatResponse.java index 3342c15..b5acf0f 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/StatResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/StatResponse.java @@ -47,7 +47,7 @@ public class StatResponse extends ResponseDataObject { } public String getProgress() { - return getOneElementValue("progress"); + return getOneValue("progress"); } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Target.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Target.java index 9d5e8fa..8942662 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/Target.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/Target.java @@ -7,27 +7,27 @@ public class Target extends ResponseDataObject { private static final long serialVersionUID = 3343881183545520108L; public String getId () { - return getOneElementValue("id"); + return getOneValue("id"); } public String getName() { - return getOneElementValue("name"); + return getOneValue("name"); } public String getHits() { - return getOneElementValue("hits"); + return getOneValue("hits"); } public String getDiagnostic() { - return getOneElementValue("diagnostic"); + return getOneValue("diagnostic"); } public String getRecords() { - return getOneElementValue("records"); + return getOneValue("records"); } public String getState () { - return getOneElementValue("state"); + return getOneValue("state"); } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/AuthResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/AuthResponse.java index 4f5b99f..243d949 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/AuthResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/AuthResponse.java @@ -5,19 +5,19 @@ public class AuthResponse extends SpResponseDataObject { private static final long serialVersionUID = 8006774126022849936L; public String getDisplayName () { - return getOneElementValue("displayName"); + return getOneValue("displayName"); } public String getRealm () { - return getOneElementValue("realm"); + return getOneValue("realm"); } public String getStatus () { - return getOneElementValue("status"); + return getOneValue("status"); } public String getAuthenticationType () { - return getOneElementValue("type"); + return getOneValue("type"); } public String onSuccess(String navigateTo) { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/TargetCategory.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/TargetCategory.java index 3885792..fd00a23 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/TargetCategory.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/sp/TargetCategory.java @@ -7,11 +7,11 @@ public class TargetCategory extends ResponseDataObject { private static final long serialVersionUID = -3027515807117682584L; public String getCategoryName () { - return getOneElementValue("categoryName"); + return getOneValue("categoryName"); } public String getCategoryId() { - return getOneElementValue("categoryId"); + return getOneValue("categoryId"); } } diff --git a/src/main/java/com/indexdata/mkjsf/utils/Utils.java b/src/main/java/com/indexdata/mkjsf/utils/Utils.java index 4b09747..3d57494 100644 --- a/src/main/java/com/indexdata/mkjsf/utils/Utils.java +++ b/src/main/java/com/indexdata/mkjsf/utils/Utils.java @@ -1,5 +1,10 @@ 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"); @@ -21,4 +26,12 @@ public class Utils { return objectId(objName); } } + + public static String concat(String... strings) { + StringBuilder concatenated = new StringBuilder(""); + for (String string : strings) { + concatenated.append(string); + } + return concatenated.toString(); + } }