Skip to content

Commit 5f30bd2

Browse files
Grid State implementation moved to GeneXusCore module.
Issue 74024
1 parent 592ec6f commit 5f30bd2

File tree

3 files changed

+146
-35
lines changed

3 files changed

+146
-35
lines changed
Lines changed: 109 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package com.genexus.webpanels.gridstate ;
2+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
3+
import com.fasterxml.jackson.annotation.PropertyAccessor;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
25
import com.genexus.*;
36
import com.genexus.diagnostics.core.ILogger;
47
import com.genexus.diagnostics.core.LogManager;
8+
import com.genexus.internet.HttpContext;
9+
import com.genexus.internet.HttpRequest;
510
import com.genexus.webpanels.GXWebObjectBase;
11+
import com.genexus.webpanels.WebSession;
612
import com.genexus.xml.GXXMLSerializable;
713

14+
import java.io.IOException;
15+
import java.lang.reflect.Constructor;
16+
817
public final class GXGridStateHandler {
918
public static final ILogger logger = LogManager.getLogger(GXGridStateHandler.class);
1019
private String gridName;
@@ -14,17 +23,26 @@ public final class GXGridStateHandler {
1423
private String varsToStateMethod;
1524
private Object parent;
1625
private ModelContext context;
17-
private GXXMLSerializable state;
26+
private GridState state;
27+
private GXXMLSerializable exposedSdtGridState;
28+
private boolean dirty;
29+
private ObjectMapper objectMapper;
30+
31+
static final String SDTGridStateClass = "com.genexuscore.genexus.common.SdtGridState";
1832

1933
private GXGridStateHandler(ModelContext context, String gridName, String programName) {
2034
this.context = context;
2135
this.gridName = programName + "_" + gridName + "_GridState";
36+
objectMapper = new ObjectMapper();
37+
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
38+
dirty =true;
2239
}
2340

2441
public GXGridStateHandler(ModelContext context, String gridName, String programName, Runnable varsFromState, Runnable varsToState) {
2542
this(context, gridName, programName);
2643
this.varsFromState = varsFromState;
2744
this.varsToState = varsToState;
45+
this.state = new GridState();
2846
}
2947

3048
//Cosntructor por java <= 1.7
@@ -33,67 +51,123 @@ public GXGridStateHandler(ModelContext context, String gridName, String programN
3351
this.varsFromStateMethod = varsFromStateMethod;
3452
this.varsToStateMethod = varsToStateMethod;
3553
this.parent = parent;
54+
this.state = new GridState();
3655
}
3756

38-
private void runVarsToState() {
39-
if (this.varsToState != null)
40-
varsToState.run();
41-
else {
42-
try {
43-
parent.getClass().getMethod(varsToStateMethod).invoke(parent, (Object[]) null);
44-
} catch (Exception ex) {
45-
logger.error("Error invoking method " + varsToStateMethod + " for grid state " + gridName, ex);
46-
}
47-
}
57+
public void saveGridState() {
58+
WebSession session = ((HttpContext) context.getHttpContext()).getWebSession();
59+
stateFromJson(session.getValue(gridName));
60+
runVarsToState();
61+
session.setValue(gridName, stateToJson());
62+
dirty = true;
4863
}
4964

50-
private void runVarsFromState() {
51-
if (this.varsFromState != null)
52-
varsFromState.run();
53-
else {
54-
try {
55-
parent.getClass().getMethod(varsFromStateMethod).invoke(parent, (Object[]) null);
56-
} catch (Exception ex) {
57-
logger.error("Error invoking method " + varsFromStateMethod + " for grid state " + gridName, ex);
58-
}
65+
public void loadGridState() {
66+
HttpContext httpContext = (HttpContext) context.getHttpContext();
67+
HttpRequest httpRequest = httpContext.getHttpRequest();
68+
if (GXutil.strcmp(httpRequest.getMethod(), "GET") == 0) {
69+
WebSession session = httpContext.getWebSession();
70+
stateFromJson(session.getValue(gridName));
71+
runVarsFromState();
72+
dirty = true;
5973
}
6074
}
6175

62-
public String filterValues(int idx) {
63-
return "";
76+
private String stateToJson() {
77+
try {
78+
return objectMapper.writeValueAsString(state);
79+
} catch (IOException ex) {
80+
logger.error("stateToJson error", ex);
81+
return null;
82+
}
6483
}
6584

66-
public void clearFilterValues() {
85+
private void stateFromJson(String json) {
86+
try {
87+
state = objectMapper.readValue(json, GridState.class);
88+
} catch (IOException ex) {
89+
logger.error("stateFromJson error", ex);
90+
}
91+
}
6792

93+
public String filterValues(int idx) {
94+
return state.InputValues.get(idx - 1).Value;
6895
}
6996

70-
public void addFilterValue(String name, String value) {
97+
public int getCurrentpage() {
98+
return state.CurrentPage;
7199
}
72100

73-
public void saveGridState() {
101+
public void setCurrentpage(int value) {
102+
state.CurrentPage = value;
103+
dirty = true;
74104
}
75105

76-
public void loadGridState() {
106+
public GXXMLSerializable getState() {
107+
try {
108+
if (dirty || exposedSdtGridState == null) {
109+
Class sdtGridStateClass = Class.forName(SDTGridStateClass);
110+
Class[] parTypes = new Class[]{ModelContext.class};
111+
Constructor ctr = sdtGridStateClass.getConstructor(parTypes);
112+
Object[] argList = new Object[]{context};
113+
HttpContext httpContext = (HttpContext) context.getHttpContext();
114+
WebSession session = httpContext.getWebSession();
115+
exposedSdtGridState = (GXXMLSerializable) ctr.newInstance(argList);
116+
exposedSdtGridState.fromJSonString(session.getValue(gridName));
117+
dirty = false;
118+
}
119+
return exposedSdtGridState;
120+
} catch (Exception ex) {
121+
logger.error("Can't create " + SDTGridStateClass, ex);
122+
return null;
123+
}
77124
}
78125

79-
public int getFiltercount() {
80-
return 0;
126+
public void setState(GXXMLSerializable state) {
127+
this.exposedSdtGridState = state;
128+
stateFromJson(exposedSdtGridState.toJSonString());
129+
dirty = true;
81130
}
82131

83-
public int getCurrentpage() {
84-
return 0;
132+
public void clearFilterValues() {
133+
state.InputValues.clear();
134+
dirty = true;
85135
}
86136

87-
public void setCurrentpage(int value) {
137+
public void addFilterValue(String name, String value) {
138+
GridStateInputValuesItem item = new GridStateInputValuesItem();
139+
item.Value = value;
140+
item.Name = name;
141+
state.InputValues.add(0, item);
142+
dirty = true;
143+
}
88144

145+
public int getFiltercount() {
146+
return state.InputValues.size();
89147
}
90148

91-
public GXXMLSerializable getState() {
92-
return this.state;
149+
private void runVarsToState() {
150+
if (this.varsToState != null)
151+
varsToState.run();
152+
else {
153+
try {
154+
parent.getClass().getMethod(varsToStateMethod).invoke(parent, (Object[]) null);
155+
} catch (Exception ex) {
156+
logger.error("Error invoking method " + varsToStateMethod + " for grid state " + gridName, ex);
157+
}
158+
}
93159
}
94160

95-
public void setState(GXXMLSerializable state) {
96-
this.state = state;
161+
private void runVarsFromState() {
162+
if (this.varsFromState != null)
163+
varsFromState.run();
164+
else {
165+
try {
166+
parent.getClass().getMethod(varsFromStateMethod).invoke(parent, (Object[]) null);
167+
} catch (Exception ex) {
168+
logger.error("Error invoking method " + varsFromStateMethod + " for grid state " + gridName, ex);
169+
}
170+
}
97171
}
98172
}
99173

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.genexus.webpanels.gridstate ;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class GridState {
7+
public GridState(int currentPage, int orderedBy, List<GridStateInputValuesItem> inputValues){
8+
this();
9+
CurrentPage =currentPage;
10+
OrderedBy = orderedBy;
11+
if( inputValues!=null)
12+
InputValues = inputValues;
13+
}
14+
public GridState(){
15+
InputValues = new ArrayList<GridStateInputValuesItem>();
16+
}
17+
protected int CurrentPage;
18+
protected int OrderedBy;
19+
protected List<GridStateInputValuesItem> InputValues;
20+
}
21+
22+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.genexus.webpanels.gridstate ;
2+
3+
public class GridStateInputValuesItem {
4+
public GridStateInputValuesItem(String name, String value) {
5+
Name = name;
6+
Value = value;
7+
}
8+
9+
public GridStateInputValuesItem() {
10+
}
11+
12+
protected String Name;
13+
protected String Value;
14+
}
15+

0 commit comments

Comments
 (0)