Skip to content

Commit 9591c73

Browse files
authored
7368: Conditional Formats of Fields (#5)
1 parent 980ff27 commit 9591c73

File tree

5 files changed

+215
-2
lines changed

5 files changed

+215
-2
lines changed

labkey-client-api/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ buildDir = new File(project.rootProject.buildDir, "/remoteapi/labkey-api-java")
6060

6161
group "org.labkey.api"
6262

63-
version "1.2.1-SNAPSHOT"
63+
version "1.3.0-SNAPSHOT"
6464

6565
dependencies {
6666
api "org.apache.httpcomponents:httpmime:${httpmimeVersion}"
@@ -250,7 +250,7 @@ if (project.hasProperty('bintray_user')
250250
name = project.name
251251
userOrg = 'labkey'
252252
licenses = ['Apache-2.0']
253-
vcsUrl = 'https://svn.mgt.labkey.host/stedi/'
253+
vcsUrl = 'https://github.com/LabKey/labkey-api-java'
254254
version {
255255
name = project.version
256256
desc = "LabKey Java Client API ${project.version}"
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.labkey.remoteapi.domain;
2+
3+
import org.labkey.remoteapi.query.Filter;
4+
5+
public class ConditionalFormatFilter extends Filter
6+
{
7+
public ConditionalFormatFilter(String value, Operator op)
8+
{
9+
super("format.column", value, op);
10+
}
11+
}

labkey-client-api/src/org/labkey/remoteapi/domain/PropertyDescriptor.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package org.labkey.remoteapi.domain;
22

3+
import org.json.simple.JSONArray;
34
import org.json.simple.JSONObject;
45
import org.labkey.remoteapi.ResponseObject;
56

7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.List;
10+
611
public class PropertyDescriptor extends ResponseObject
712
{
813
private String _name;
@@ -21,6 +26,7 @@ public class PropertyDescriptor extends ResponseObject
2126
private String _lookupSchema;
2227
private String _lookupQuery;
2328
private String _lookupContainer;
29+
private List<ConditionalFormat> _conditionalFormats = new ArrayList<>();
2430

2531
public PropertyDescriptor()
2632
{
@@ -67,6 +73,12 @@ public PropertyDescriptor(JSONObject json)
6773
_propertyURI = (String)json.get("propertyURI");
6874
_rangeURI = (String)json.get("rangeURI");
6975

76+
JSONArray cfs = (JSONArray) json.get("conditionalFormats");
77+
cfs.forEach(cf -> {
78+
JSONObject cfObj = (JSONObject) cf;
79+
_conditionalFormats.add(ConditionalFormat.fromJSON(cfObj));
80+
});
81+
7082
if (json.get("lookupSchema") != null)
7183
_lookupSchema = (String)json.get("lookupSchema");
7284
if (json.get("lookupQuery") != null)
@@ -101,6 +113,7 @@ public JSONObject toJSONObject(boolean forProtocol)
101113
result.put("mvEnabled", _mvEnabled);
102114
result.put("dimension", _dimension);
103115
result.put("propertyURI", _propertyURI);
116+
result.put("conditionalFormats", serializeConditionalFormats());
104117

105118
if (_rangeURI != null)
106119
result.put("rangeURI", _rangeURI);
@@ -123,6 +136,17 @@ public JSONObject toJSONObject(boolean forProtocol)
123136
return result;
124137
}
125138

139+
private JSONArray serializeConditionalFormats()
140+
{
141+
JSONArray cfs = new JSONArray();
142+
for (ConditionalFormat conditionalFormat : _conditionalFormats)
143+
{
144+
JSONObject cf = conditionalFormat.toJSON();
145+
cfs.add(cf);
146+
}
147+
return cfs;
148+
}
149+
126150
/**
127151
* Convenience function for creating lookups
128152
* @param schema the schema for the lookup
@@ -279,4 +303,15 @@ public PropertyDescriptor setMvEnabled(Boolean mvEnabled)
279303
_mvEnabled = mvEnabled;
280304
return this;
281305
}
306+
307+
public PropertyDescriptor setConditionalFormats(List<ConditionalFormat> conditionalFormats)
308+
{
309+
_conditionalFormats = conditionalFormats;
310+
return this;
311+
}
312+
313+
public List<ConditionalFormat> getConditionalFormats()
314+
{
315+
return Collections.unmodifiableList(_conditionalFormats);
316+
}
282317
}

labkey-client-api/src/org/labkey/remoteapi/query/Filter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ public enum Operator
109109
_programmaticNameToOperator.put(o.getProgrammaticName(), o);
110110
}
111111

112+
private static final Map<String, Operator> _urlKeyToOperator = new HashMap<>(Operator.values().length);
113+
114+
static
115+
{
116+
for (Operator o : Operator.values())
117+
_urlKeyToOperator.put(o.getUrlKey(), o);
118+
}
119+
112120
private final String _urlKey;
113121
private final String _displayValue;
114122
private final String _programmaticName;
@@ -148,6 +156,11 @@ public static Operator getOperator(String programmaticName)
148156
return _programmaticNameToOperator.get(programmaticName);
149157
}
150158

159+
public static Operator getOperatorFromUrlKey(String urlKey)
160+
{
161+
return _urlKeyToOperator.get(urlKey);
162+
}
163+
151164
@Deprecated // Use getDisplayValue()... this method is for backward compatibility
152165
public String getCaption()
153166
{

0 commit comments

Comments
 (0)