|
| 1 | +package org.labkey.remoteapi.domain; |
| 2 | + |
| 3 | +import org.json.simple.JSONObject; |
| 4 | +import org.labkey.remoteapi.query.Filter; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +public class ConditionalFormat |
| 10 | +{ |
| 11 | + private List<ConditionalFormatFilter> _queryFilter; |
| 12 | + private String _textColor; |
| 13 | + private String _backgroundColor; |
| 14 | + private Boolean _bold; |
| 15 | + private Boolean _italic; |
| 16 | + private Boolean _strikethrough; |
| 17 | + |
| 18 | + public ConditionalFormat(List<ConditionalFormatFilter> filters, String textColor, String backgroundColor, Boolean bold, Boolean italic, Boolean strikethrough) |
| 19 | + { |
| 20 | + _queryFilter = filters; |
| 21 | + _textColor = (null == textColor) ? "" : textColor; |
| 22 | + _backgroundColor = (null == backgroundColor) ? "" : backgroundColor; |
| 23 | + _bold = (null == bold) ? false : bold; |
| 24 | + _italic = (null == italic) ? false : italic; |
| 25 | + _strikethrough = (null == strikethrough) ? false : strikethrough; |
| 26 | + } |
| 27 | + |
| 28 | + public ConditionalFormat(List<ConditionalFormatFilter> filters, String textColor, String backgroundColor) |
| 29 | + { |
| 30 | + this(filters, textColor, backgroundColor, null, null, null); |
| 31 | + } |
| 32 | + |
| 33 | + public ConditionalFormat(List<ConditionalFormatFilter> filters, Boolean bold, Boolean italic, Boolean strikethrough) |
| 34 | + { |
| 35 | + this(filters, null, null, bold, italic, strikethrough); |
| 36 | + } |
| 37 | + |
| 38 | + public String queryFilterToJSONString() |
| 39 | + { |
| 40 | + String delim = ""; |
| 41 | + StringBuilder sb = new StringBuilder(); |
| 42 | + |
| 43 | + for (Filter f : _queryFilter) |
| 44 | + { |
| 45 | + sb.append(delim); |
| 46 | + sb.append(f.getQueryStringParamName()).append("=").append(f.getQueryStringParamValue()); |
| 47 | + delim = "&"; |
| 48 | + |
| 49 | + } |
| 50 | + return sb.toString(); |
| 51 | + } |
| 52 | + |
| 53 | + public JSONObject toJSON() |
| 54 | + { |
| 55 | + JSONObject conditionalFormat = new JSONObject(); |
| 56 | + conditionalFormat.put("filter", queryFilterToJSONString()); |
| 57 | + conditionalFormat.put("backgroundColor", _backgroundColor); |
| 58 | + conditionalFormat.put("bold", _bold); |
| 59 | + conditionalFormat.put("strikethrough", _strikethrough); |
| 60 | + conditionalFormat.put("italic", _italic); |
| 61 | + conditionalFormat.put("textColor", _textColor); |
| 62 | + return conditionalFormat; |
| 63 | + } |
| 64 | + |
| 65 | + static public List<ConditionalFormatFilter> queryFilterFromJSONString(String filterStr) |
| 66 | + { |
| 67 | + List<ConditionalFormatFilter> queryFilter = new ArrayList<>(); |
| 68 | + |
| 69 | + String[] filterStrs = filterStr.split("&"); |
| 70 | + for (String filter : filterStrs) |
| 71 | + { |
| 72 | + String filterExp = filter.substring(filter.indexOf("~") + 1, filter.indexOf("=")); |
| 73 | + Filter.Operator op = Filter.Operator.getOperatorFromUrlKey(filterExp); |
| 74 | + |
| 75 | + String value = filter.substring(filter.lastIndexOf("=") + 1); |
| 76 | + queryFilter.add(new ConditionalFormatFilter(value, op)); |
| 77 | + } |
| 78 | + |
| 79 | + return queryFilter; |
| 80 | + } |
| 81 | + |
| 82 | + static public ConditionalFormat fromJSON(JSONObject json) |
| 83 | + { |
| 84 | + String filterStr = (String) json.get("filter"); |
| 85 | + return new ConditionalFormat( |
| 86 | + queryFilterFromJSONString(filterStr), |
| 87 | + (String) json.get("textColor"), |
| 88 | + (String) json.get("backgroundColor"), |
| 89 | + (Boolean) json.get("bold"), |
| 90 | + (Boolean) json.get("italic"), |
| 91 | + (Boolean) json.get("strikethrough") |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + public List<ConditionalFormatFilter> getQueryFilter() |
| 96 | + { |
| 97 | + return _queryFilter; |
| 98 | + } |
| 99 | + |
| 100 | + public void setQueryFilter(List<ConditionalFormatFilter> filters) |
| 101 | + { |
| 102 | + _queryFilter = filters; |
| 103 | + } |
| 104 | + |
| 105 | + public String getTextColor() |
| 106 | + { |
| 107 | + return _textColor; |
| 108 | + } |
| 109 | + |
| 110 | + public void setTextColor(String textColor) |
| 111 | + { |
| 112 | + _textColor = textColor; |
| 113 | + } |
| 114 | + |
| 115 | + public String getBackgroundColor() |
| 116 | + { |
| 117 | + return _backgroundColor; |
| 118 | + } |
| 119 | + |
| 120 | + public void setBackgroundColor(String backgroundColor) |
| 121 | + { |
| 122 | + _backgroundColor = backgroundColor; |
| 123 | + } |
| 124 | + |
| 125 | + public Boolean getBold() |
| 126 | + { |
| 127 | + return _bold; |
| 128 | + } |
| 129 | + |
| 130 | + public void setBold(Boolean bold) |
| 131 | + { |
| 132 | + _bold = bold; |
| 133 | + } |
| 134 | + |
| 135 | + public Boolean getItalic() |
| 136 | + { |
| 137 | + return _italic; |
| 138 | + } |
| 139 | + |
| 140 | + public void setItalic(Boolean italic) |
| 141 | + { |
| 142 | + _italic = italic; |
| 143 | + } |
| 144 | + |
| 145 | + public Boolean getStrikethrough() |
| 146 | + { |
| 147 | + return _strikethrough; |
| 148 | + } |
| 149 | + |
| 150 | + public void setStrikethrough(Boolean strikethrough) |
| 151 | + { |
| 152 | + _strikethrough = strikethrough; |
| 153 | + } |
| 154 | +} |
0 commit comments