Skip to content

Commit 7dafdac

Browse files
committed
[KNOWAGE-9289] Refactor getProperties to use layer label instead of ID
1 parent f55d952 commit 7dafdac

3 files changed

Lines changed: 31 additions & 38 deletions

File tree

knowage-core/src/main/java/it/eng/spagobi/mapcatalogue/service/LayerCRUD.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,36 +113,33 @@ public String loadLayers(@Context HttpServletRequest req) throws JSONException,
113113
@Produces(MediaType.APPLICATION_JSON + "; charset=UTF-8")
114114
@UserConstraint(functionalities = { CommunityFunctionalityConstants.GEO_LAYERS_MANAGEMENT })
115115
public String getFilter(@Context HttpServletRequest req) throws JSONException {
116-
Object id = null;
117-
Integer layerId = null;
116+
String layerLabel;
118117

119118
try {
120-
121-
id = req.getParameter("id");
122-
if (id == null || id.equals("")) {
119+
layerLabel = req.getParameter("label");
120+
if (layerLabel == null || layerLabel.isEmpty()) {
123121
throw new SpagoBIRuntimeException("The layer id passed in the request is null or empty");
124122
}
125-
layerId = new Integer(id.toString());
126123
} catch (Exception e) {
127124
logger.error("error loading filter", e);
128125
throw new SpagoBIRuntimeException("error request", e);
129126
}
130127

131128
ISbiGeoLayersDAO dao = DAOFactory.getSbiGeoLayerDao();
132-
ArrayList<String> properties = null;
129+
ArrayList<String> properties;
133130
try {
134-
properties = dao.getProperties(layerId);
131+
properties = dao.getProperties(layerLabel);
135132
} catch (Exception e2) {
136-
logger.error("Error loading the properties for the layer with id " + id, e2);
137-
throw new SpagoBIRuntimeException("Error loading the properties for the layer with id [" + id
133+
logger.error("Error loading the properties for the layer with layer " + layerLabel, e2);
134+
throw new SpagoBIRuntimeException("Error loading the properties for the layer with layer [" + layerLabel
138135
+ "]. Please check the layer configuration or the file content.", e2);
139136
}
140137
ArrayList<JSONObject> prop = new ArrayList<>();
141-
for (int i = 0; i < properties.size(); i++) {
142-
JSONObject obj = new JSONObject();
143-
obj.put("property", properties.get(i));
144-
prop.add(obj);
145-
}
138+
for (String property : properties) {
139+
JSONObject obj = new JSONObject();
140+
obj.put("property", property);
141+
prop.add(obj);
142+
}
146143

147144
return prop.toString();
148145
}

knowagedao/src/main/java/it/eng/spagobi/mapcatalogue/dao/ISbiGeoLayersDAO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public interface ISbiGeoLayersDAO extends ISpagoBIDao {
4747

4848
public Integer insertLayer(GeoLayer aLayer) throws EMFUserError, JSONException, UnsupportedEncodingException, IOException;
4949

50-
public ArrayList<String> getProperties(int layerId);
50+
public ArrayList<String> getProperties(String layerLabel);
5151

5252
public void eraseLayer(Integer layerId) throws EMFUserError, JSONException;
5353

knowagedao/src/main/java/it/eng/spagobi/mapcatalogue/dao/SbiGeoLayersDAOHibImpl.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,13 @@ public Criteria evaluate(Session session) {
442442
}
443443

444444
@Override
445-
public ArrayList<String> getProperties(int layerId) {
445+
public ArrayList<String> getProperties(String layerLabel) {
446446
Session tmpSession = null;
447447
ArrayList<String> keys = new ArrayList<>();
448448
try {
449449
tmpSession = getSession();
450450
tmpSession.beginTransaction();
451-
GeoLayer aLayer = loadLayerByID(layerId);
451+
GeoLayer aLayer = loadLayerByLabel(layerLabel);
452452
JSONObject layerDef = new JSONObject(new String(aLayer.getLayerDef()));
453453
if (aLayer.getType().equals("Google") || aLayer.getType().equals("TMS") || aLayer.getType().equals("OSM")) {
454454
return new ArrayList<>();
@@ -469,16 +469,7 @@ public ArrayList<String> getProperties(int layerId) {
469469
if (c != null) {
470470
JSONObject obj = new JSONObject(c);
471471
content = obj.getJSONArray("features");
472-
for (int j = 0; j < content.length(); j++) {
473-
obj = content.getJSONObject(j).getJSONObject(PROPERTIES);
474-
Iterator<String> it = obj.keys();
475-
while (it.hasNext()) {
476-
String key = it.next();
477-
if (!keys.contains(key)) {
478-
keys.add(key);
479-
}
480-
}
481-
}
472+
iterateFeatures(keys, content);
482473
}
483474

484475
} while (c != null);
@@ -548,16 +539,7 @@ else if (aLayer.getType().equals("WFS") || aLayer.getType().equals("WMS")) {
548539
JSONObject itaAdm1 = objects.getJSONObject(childObjects[0]);
549540
content = itaAdm1.getJSONArray("geometries");
550541

551-
for (int j = 0; j < content.length(); j++) {
552-
obj = content.getJSONObject(j).getJSONObject(PROPERTIES);
553-
Iterator<String> it = obj.keys();
554-
while (it.hasNext()) {
555-
String key = it.next();
556-
if (!keys.contains(key)) {
557-
keys.add(key);
558-
}
559-
}
560-
}
542+
iterateFeatures(keys, content);
561543

562544
}
563545

@@ -574,6 +556,20 @@ else if (aLayer.getType().equals("WFS") || aLayer.getType().equals("WMS")) {
574556
return keys;
575557
}
576558

559+
private void iterateFeatures(ArrayList<String> keys, JSONArray content) throws JSONException {
560+
JSONObject obj;
561+
for (int j = 0; j < content.length(); j++) {
562+
obj = content.getJSONObject(j).getJSONObject(PROPERTIES);
563+
Iterator<String> it = obj.keys();
564+
while (it.hasNext()) {
565+
String key = it.next();
566+
if (!keys.contains(key)) {
567+
keys.add(key);
568+
}
569+
}
570+
}
571+
}
572+
577573
private URL getURL(GeoLayer aLayer) throws Exception {
578574

579575
String resourcePath = SpagoBIUtilities.getResourcePath();

0 commit comments

Comments
 (0)