gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.lib / org.gvsig.online.lib.impl / src / main / java / org / gvsig / online / lib / impl / OnlineProjectImpl.java @ 9515
History | View | Annotate | Download (8.12 KB)
1 |
package org.gvsig.online.lib.impl; |
---|---|
2 |
|
3 |
import org.gvsig.online.lib.api.OnlineDownloader; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.net.URL; |
7 |
import java.util.Iterator; |
8 |
import java.util.function.Predicate; |
9 |
import javax.json.JsonArray; |
10 |
import org.gvsig.online.lib.api.OnlineProject; |
11 |
import javax.json.JsonNumber; |
12 |
import javax.json.JsonObject; |
13 |
import javax.json.JsonValue; |
14 |
import org.apache.commons.io.IOUtils; |
15 |
import org.apache.commons.lang3.StringUtils; |
16 |
import org.gvsig.fmap.geom.Geometry; |
17 |
import org.gvsig.fmap.geom.GeometryUtils; |
18 |
import org.gvsig.fmap.geom.primitive.Envelope; |
19 |
import org.gvsig.json.Json; |
20 |
import org.gvsig.json.JsonObjectBuilder; |
21 |
import org.gvsig.online.lib.api.OnlineLayer; |
22 |
import org.gvsig.online.lib.api.OnlineLocator; |
23 |
import org.gvsig.online.lib.api.OnlineManager; |
24 |
import org.gvsig.tools.ToolsLocator; |
25 |
import org.gvsig.tools.i18n.I18nManager; |
26 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
27 |
import org.gvsig.tools.task.SimpleTaskStatus; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
@SuppressWarnings("UseSpecificCatch") |
34 |
public class OnlineProjectImpl implements OnlineProject { |
35 |
|
36 |
private JsonObject data;
|
37 |
private Envelope envelope = null; |
38 |
private OnlineSiteImpl site;
|
39 |
private JsonObject entities_json;
|
40 |
|
41 |
public OnlineProjectImpl() {
|
42 |
|
43 |
} |
44 |
|
45 |
OnlineProjectImpl setSite(OnlineSiteImpl site) { |
46 |
this.site = site;
|
47 |
return this; |
48 |
} |
49 |
|
50 |
@Override
|
51 |
public void fromJson(JsonObject data) { |
52 |
JsonObject theSite_json = data.getJsonObject("site");
|
53 |
if (theSite_json != null) { |
54 |
OnlineSiteImpl theSite = new OnlineSiteImpl();
|
55 |
theSite.fromJson(theSite_json); |
56 |
this.data = data.getJsonObject("project"); |
57 |
this.site = theSite;
|
58 |
} else {
|
59 |
this.data = data;
|
60 |
} |
61 |
} |
62 |
|
63 |
@Override
|
64 |
public JsonObjectBuilder toJsonBuilder() {
|
65 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
66 |
builder.add_class(this);
|
67 |
builder.add("site", this.site.toJsonBuilder()); |
68 |
builder.add("project", (JsonValue) data);
|
69 |
return builder;
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
public int getId() { |
74 |
return this.data.getInt("id", -1); |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public String getName() { |
79 |
return this.data.getString("name", null); |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public String getTitle() { |
84 |
return this.data.getString("title", null); |
85 |
} |
86 |
|
87 |
@Override
|
88 |
public String getLabel() { |
89 |
if (StringUtils.isNotBlank(this.getTitle())) { |
90 |
return this.getTitle(); |
91 |
} |
92 |
if (StringUtils.isNotBlank(this.getName())) { |
93 |
return this.getName(); |
94 |
} |
95 |
return String.valueOf(this.getId()); |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public String getDescription() { |
100 |
return this.data.getString("description", null); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public Envelope getEnvelope() {
|
105 |
if (this.envelope == null) { |
106 |
JsonNumber extent4326_minx = this.data.getJsonNumber("extent4326_minx"); |
107 |
JsonNumber extent4326_miny = this.data.getJsonNumber("extent4326_miny"); |
108 |
JsonNumber extent4326_maxx = this.data.getJsonNumber("extent4326_maxx"); |
109 |
JsonNumber extent4326_maxy = this.data.getJsonNumber("extent4326_maxy"); |
110 |
if (extent4326_minx == null || extent4326_miny == null |
111 |
|| extent4326_maxx == null || extent4326_maxy == null) { |
112 |
return null; |
113 |
} |
114 |
this.envelope = GeometryUtils.createEnvelope(
|
115 |
extent4326_minx.doubleValue(), |
116 |
extent4326_miny.doubleValue(), |
117 |
extent4326_maxx.doubleValue(), |
118 |
extent4326_maxy.doubleValue(), |
119 |
Geometry.SUBTYPES.GEOM2D |
120 |
); |
121 |
} |
122 |
return this.envelope; |
123 |
} |
124 |
|
125 |
private JsonObject getLayers(SimpleTaskStatus status) {
|
126 |
if (this.entities_json == null) { |
127 |
if (this.site == null) { |
128 |
return null; |
129 |
} |
130 |
FileInputStream fis = null; |
131 |
try {
|
132 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
133 |
SimpleTaskStatus.push(status); |
134 |
SimpleTaskStatus.setIndeterminate(status); |
135 |
SimpleTaskStatus.message(status, i18n.getTranslation("_Download_project_layers_information"));
|
136 |
URL url = this.site.getURL("/api/v1/projects/" + this.getId() + "/layers"); |
137 |
OnlineDownloader downloader = this.site.getDownloader();
|
138 |
File f = downloader.get(url);
|
139 |
fis = new FileInputStream(f); |
140 |
JsonObject json = Json.createObject(fis); |
141 |
this.entities_json = json;
|
142 |
SimpleTaskStatus.terminate(status); |
143 |
} catch (Exception ex) { |
144 |
throw new RuntimeException("Can't download layers", ex); |
145 |
} finally {
|
146 |
IOUtils.closeQuietly(fis); |
147 |
SimpleTaskStatus.pop(status); |
148 |
} |
149 |
} |
150 |
return this.entities_json; |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public Iterable<OnlineLayer> layers() { |
155 |
return this.layers(null); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public Iterable<OnlineLayer> layers(SimpleTaskStatus status) { |
160 |
OnlineProjectImpl theProject = this;
|
161 |
@SuppressWarnings("Convert2Lambda") |
162 |
Iterable<OnlineLayer> iterable = new Iterable<OnlineLayer>() { |
163 |
@Override
|
164 |
public Iterator<OnlineLayer> iterator() { |
165 |
JsonArray layers = getLayers(status).getJsonArray("content");
|
166 |
Iterator<JsonValue> it = layers.iterator();
|
167 |
return new Iterator<OnlineLayer>() { |
168 |
@Override
|
169 |
public boolean hasNext() { |
170 |
return it.hasNext();
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public OnlineLayer next() {
|
175 |
JsonObject layer_json = (JsonObject) it.next(); |
176 |
return new OnlineLayerImpl(theProject, layer_json); |
177 |
} |
178 |
}; |
179 |
} |
180 |
}; |
181 |
return iterable;
|
182 |
} |
183 |
|
184 |
public OnlineLayer getLayer(Predicate<OnlineLayer> filter, SimpleTaskStatus status) { |
185 |
JsonArray layers = getLayers(status).getJsonArray("content");
|
186 |
for (JsonValue layer0 : layers) {
|
187 |
OnlineLayerImpl layer = new OnlineLayerImpl(site, (JsonObject) layer0);
|
188 |
if (filter.test(layer)) {
|
189 |
return layer;
|
190 |
} |
191 |
} |
192 |
return null; |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public OnlineSiteImpl getSite() {
|
197 |
return this.site; |
198 |
} |
199 |
|
200 |
public String getConfigTableName() { |
201 |
return "GVSIGD_CONFIG_" + this.getId(); |
202 |
} |
203 |
|
204 |
public String getResourcesTablaName() { |
205 |
return "GVSIGD_RESOURCES_" + this.getId(); |
206 |
} |
207 |
|
208 |
public OnlineLayer getConfigTable(SimpleTaskStatus status) {
|
209 |
return this.getLayer((OnlineLayer t) -> StringUtils.equalsIgnoreCase(t.getName(), this.getConfigTableName()), status); |
210 |
} |
211 |
|
212 |
public OnlineLayer getResourcesTable(SimpleTaskStatus status) {
|
213 |
return this.getLayer((OnlineLayer t) -> StringUtils.equalsIgnoreCase(t.getName(), this.getResourcesTablaName()), status); |
214 |
} |
215 |
|
216 |
public static void selfRegister() { |
217 |
Json.registerSerializer(OnlineProjectImpl.class); |
218 |
} |
219 |
|
220 |
|
221 |
public static void main(String[] args) throws Exception { |
222 |
new DefaultLibrariesInitializer().fullInitialize();
|
223 |
|
224 |
String s;
|
225 |
String siteURL = "https://devel.gvsigonline.com/gvsigonline"; |
226 |
OnlineManager manager = OnlineLocator.getOnlineManager(); |
227 |
TestUserIdentificationRequesterFactory userIdentificationRequesterFactory = new TestUserIdentificationRequesterFactory(siteURL);
|
228 |
manager.addUserIdentificationRequester(userIdentificationRequesterFactory); |
229 |
OnlineSiteImpl site = (OnlineSiteImpl) manager.connectSite( |
230 |
userIdentificationRequesterFactory.create(), |
231 |
new URL(siteURL) |
232 |
); |
233 |
OnlineProjectImpl proj = site.getProject("jjdctestdesktop");
|
234 |
s = proj.toJson().toString(); |
235 |
System.out.println(s);
|
236 |
proj = new OnlineProjectImpl();
|
237 |
proj.fromJson(Json.createObject(s)); |
238 |
s = proj.toJson().toString(); |
239 |
System.out.println(s);
|
240 |
System.out.println("Fin"); |
241 |
} |
242 |
|
243 |
} |