Revision 9515 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

View differences:

OnlineProjectImpl.java
4 4
import java.io.File;
5 5
import java.io.FileInputStream;
6 6
import java.net.URL;
7
import java.nio.charset.StandardCharsets;
8 7
import java.util.Iterator;
9 8
import java.util.function.Predicate;
10 9
import javax.json.JsonArray;
......
12 11
import javax.json.JsonNumber;
13 12
import javax.json.JsonObject;
14 13
import javax.json.JsonValue;
15
import org.apache.commons.io.FileUtils;
16 14
import org.apache.commons.io.IOUtils;
17 15
import org.apache.commons.lang3.StringUtils;
18 16
import org.gvsig.fmap.geom.Geometry;
......
23 21
import org.gvsig.online.lib.api.OnlineLayer;
24 22
import org.gvsig.online.lib.api.OnlineLocator;
25 23
import org.gvsig.online.lib.api.OnlineManager;
26
import org.gvsig.online.lib.api.OnlineSite;
27
import org.gvsig.online.lib.api.OnlineUserIdentificationRequester;
28 24
import org.gvsig.tools.ToolsLocator;
29 25
import org.gvsig.tools.i18n.I18nManager;
30 26
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
31
import org.gvsig.tools.observer.Observer;
32 27
import org.gvsig.tools.task.SimpleTaskStatus;
33 28

  
34 29
/**
......
44 39
    private JsonObject entities_json;
45 40

  
46 41
    public OnlineProjectImpl() {
47
        
42

  
48 43
    }
49
    
44

  
50 45
    OnlineProjectImpl setSite(OnlineSiteImpl site) {
51 46
        this.site = site;
52 47
        return this;
53 48
    }
54
    
49

  
55 50
    @Override
56 51
    public void fromJson(JsonObject data) {
57 52
        JsonObject theSite_json = data.getJsonObject("site");
58
        if( theSite_json!=null ) {
53
        if (theSite_json != null) {
59 54
            OnlineSiteImpl theSite = new OnlineSiteImpl();
60 55
            theSite.fromJson(theSite_json);
61 56
            this.data = data.getJsonObject("project");
......
70 65
        JsonObjectBuilder builder = Json.createObjectBuilder();
71 66
        builder.add_class(this);
72 67
        builder.add("site", this.site.toJsonBuilder());
73
        builder.add("project", (JsonValue)data);
68
        builder.add("project", (JsonValue) data);
74 69
        return builder;
75 70
    }
76
    
77
    
71

  
78 72
    @Override
79 73
    public int getId() {
80 74
        return this.data.getInt("id", -1);
......
82 76

  
83 77
    @Override
84 78
    public String getName() {
85
        return this.data.getString("name",null);
79
        return this.data.getString("name", null);
86 80
    }
87 81

  
88 82
    @Override
89 83
    public String getTitle() {
90
        return this.data.getString("title",null);
84
        return this.data.getString("title", null);
91 85
    }
92 86

  
93 87
    @Override
94 88
    public String getLabel() {
95
        if( StringUtils.isNotBlank(this.getTitle())) {
89
        if (StringUtils.isNotBlank(this.getTitle())) {
96 90
            return this.getTitle();
97 91
        }
98
        if( StringUtils.isNotBlank(this.getName())) {
92
        if (StringUtils.isNotBlank(this.getName())) {
99 93
            return this.getName();
100 94
        }
101 95
        return String.valueOf(this.getId());
......
103 97

  
104 98
    @Override
105 99
    public String getDescription() {
106
        return this.data.getString("description",null);
100
        return this.data.getString("description", null);
107 101
    }
108
    
102

  
109 103
    @Override
110 104
    public Envelope getEnvelope() {
111
        if( this.envelope == null ) {
105
        if (this.envelope == null) {
112 106
            JsonNumber extent4326_minx = this.data.getJsonNumber("extent4326_minx");
113 107
            JsonNumber extent4326_miny = this.data.getJsonNumber("extent4326_miny");
114 108
            JsonNumber extent4326_maxx = this.data.getJsonNumber("extent4326_maxx");
115 109
            JsonNumber extent4326_maxy = this.data.getJsonNumber("extent4326_maxy");
116
            if( extent4326_minx == null || extent4326_miny == null || 
117
                    extent4326_maxx == null || extent4326_maxy == null ) {
110
            if (extent4326_minx == null || extent4326_miny == null
111
                    || extent4326_maxx == null || extent4326_maxy == null) {
118 112
                return null;
119 113
            }
120 114
            this.envelope = GeometryUtils.createEnvelope(
121
                    extent4326_minx.doubleValue(), 
122
                    extent4326_miny.doubleValue(), 
123
                    extent4326_maxx.doubleValue(), 
124
                    extent4326_maxy.doubleValue(), 
115
                    extent4326_minx.doubleValue(),
116
                    extent4326_miny.doubleValue(),
117
                    extent4326_maxx.doubleValue(),
118
                    extent4326_maxy.doubleValue(),
125 119
                    Geometry.SUBTYPES.GEOM2D
126 120
            );
127 121
        }
128 122
        return this.envelope;
129 123
    }
130
    
124

  
131 125
    private JsonObject getLayers(SimpleTaskStatus status) {
132
        if( this.entities_json == null ) {
133
            if( this.site == null ) {
126
        if (this.entities_json == null) {
127
            if (this.site == null) {
134 128
                return null;
135 129
            }
136 130
            FileInputStream fis = null;
......
138 132
                I18nManager i18n = ToolsLocator.getI18nManager();
139 133
                SimpleTaskStatus.push(status);
140 134
                SimpleTaskStatus.setIndeterminate(status);
141
                SimpleTaskStatus.message(status,i18n.getTranslation("_Download_project_layers_information"));
142
                URL url = this.site.getURL("/api/v1/projects/"+this.getId()+"/layers");
143
                OnlineDownloader downloader =this.site.getDownloader();
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();
144 138
                File f = downloader.get(url);
145 139
                fis = new FileInputStream(f);
146 140
                JsonObject json = Json.createObject(fis);
147 141
                this.entities_json = json;
148 142
                SimpleTaskStatus.terminate(status);
149
            } catch(Exception ex) {
143
            } catch (Exception ex) {
150 144
                throw new RuntimeException("Can't download layers", ex);
151 145
            } finally {
152 146
                IOUtils.closeQuietly(fis);
......
154 148
            }
155 149
        }
156 150
        return this.entities_json;
157
    }    
158
    
151
    }
152

  
159 153
    @Override
160 154
    public Iterable<OnlineLayer> layers() {
161 155
        return this.layers(null);
162 156
    }
163
    
157

  
158
    @Override
164 159
    public Iterable<OnlineLayer> layers(SimpleTaskStatus status) {
165 160
        OnlineProjectImpl theProject = this;
166 161
        @SuppressWarnings("Convert2Lambda")
......
185 180
        };
186 181
        return iterable;
187 182
    }
188
    
189
    public OnlineLayer getLayer(Predicate<OnlineLayer>filter, SimpleTaskStatus status) {
183

  
184
    public OnlineLayer getLayer(Predicate<OnlineLayer> filter, SimpleTaskStatus status) {
190 185
        JsonArray layers = getLayers(status).getJsonArray("content");
191 186
        for (JsonValue layer0 : layers) {
192
            OnlineLayerImpl layer = new OnlineLayerImpl(site, (JsonObject)layer0);
193
            if( filter.test(layer) ) {
187
            OnlineLayerImpl layer = new OnlineLayerImpl(site, (JsonObject) layer0);
188
            if (filter.test(layer)) {
194 189
                return layer;
195
            }            
190
            }
196 191
        }
197 192
        return null;
198 193
    }
......
203 198
    }
204 199

  
205 200
    public String getConfigTableName() {
206
        return "GVSIGD_CONFIG_"+this.getId();
201
        return "GVSIGD_CONFIG_" + this.getId();
207 202
    }
208
    
203

  
209 204
    public String getResourcesTablaName() {
210
        return "GVSIGD_RESOURCES_"+this.getId();
205
        return "GVSIGD_RESOURCES_" + this.getId();
211 206
    }
212
    
207

  
213 208
    public OnlineLayer getConfigTable(SimpleTaskStatus status) {
214
        return this.getLayer((OnlineLayer t) -> StringUtils.equalsIgnoreCase(t.getName(),this.getConfigTableName()), status);
209
        return this.getLayer((OnlineLayer t) -> StringUtils.equalsIgnoreCase(t.getName(), this.getConfigTableName()), status);
215 210
    }
216
    
211

  
217 212
    public OnlineLayer getResourcesTable(SimpleTaskStatus status) {
218
        return this.getLayer((OnlineLayer t) -> StringUtils.equalsIgnoreCase(t.getName(),this.getResourcesTablaName()), status);
213
        return this.getLayer((OnlineLayer t) -> StringUtils.equalsIgnoreCase(t.getName(), this.getResourcesTablaName()), status);
219 214
    }
220 215

  
221 216
    public static void selfRegister() {
222 217
        Json.registerSerializer(OnlineProjectImpl.class);
223 218
    }
224 219

  
220
    
225 221
    public static void main(String[] args) throws Exception {
226 222
        new DefaultLibrariesInitializer().fullInitialize();
227
        
228
        OnlineManager manager = OnlineLocator.getOnlineManager();
229
        manager.setUserIdentificationRequester(new OnlineUserIdentificationRequester() {
230
            @Override
231
            public boolean requestIdentification() {
232
                return true;
233
            }
234 223

  
235
            @Override
236
            public String getUserId() {
237
//                return "TheUserName";
238
                return "jjdelcerro";
239
            }
240

  
241
            @Override
242
            public String getPassword() {
243
//                return "ThePassword";
244
                return "Iefo0She";
245
            }
246

  
247
            @Override
248
            public void addObserver(Observer o) {
249
                throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
250
            }
251

  
252
            @Override
253
            public void deleteObserver(Observer o) {
254
                throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
255
            }
256

  
257
            @Override
258
            public void deleteObservers() {
259
                throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
260
            }
261
        });
262
        File f;
263 224
        String s;
264
        OnlineSiteImpl site = (OnlineSiteImpl) manager.connectSite(new URL("https://devel.gvsigonline.com/gvsigonline"));
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
        );
265 233
        OnlineProjectImpl proj = site.getProject("jjdctestdesktop");
266 234
        s = proj.toJson().toString();
267 235
        System.out.println(s);
......
271 239
        System.out.println(s);
272 240
        System.out.println("Fin");
273 241
    }
274
    
242

  
275 243
}

Also available in: Unified diff