Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extPublish / src / org / gvsig / publish / serversmodel / Service.java @ 21498

History | View | Annotate | Download (8.04 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004-2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Iba?ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.publish.serversmodel;
42

    
43
import java.util.Collection;
44
import java.util.Iterator;
45
import java.util.LinkedHashMap;
46
import java.util.Observable;
47
import java.util.Set;
48

    
49
import org.gvsig.publish.IPublishPersistence;
50
import org.gvsig.publish.PublishLogger;
51
import org.gvsig.publish.PublishRegister;
52
import org.gvsig.publish.infoproject.FilterProjectInfo;
53
import org.gvsig.publish.infoproject.ILayerInfo;
54
import org.gvsig.publish.infoproject.IProjectInfo;
55
import org.gvsig.publish.infoproject.IViewInfo;
56

    
57
import com.iver.utiles.XMLEntity;
58

    
59
/**
60
 * A server implements severals services.
61
 * 
62
 * @author jvhigon
63
 *
64
 */
65
public abstract class Service extends Observable implements IPublishPersistence {
66

    
67
        /*
68
         * HasMap with its remote resources
69
         */
70
        private LinkedHashMap remoteResources = null;
71
        //Associations
72
        private Server server = null;
73

    
74
        private IProjectInfo projectInfo;
75

    
76
        /**
77
         * Constructor
78
         * Only creates a empty hashmap with its remote resources 
79
         */
80
        public Service(Server server){
81
                remoteResources = new LinkedHashMap();
82
                this.server = server;
83
        }
84
        /**
85
         * 
86
         * @return string which identifies the service in the publish register
87
         */
88
        public abstract String getRegisterTag();
89
        /**
90
         * 
91
         * @return unique identifier which identifies a service instance 
92
         */
93
        public abstract String getId();
94
        /**
95
         * 
96
         * @return Filter to apply to project information 
97
         */
98
        public abstract FilterProjectInfo getFilter();
99
        /**
100
         * 
101
         * @return project information of this service. It can be filtered.
102
         */
103
        public IProjectInfo getProjectInfo(){
104
                if (this.projectInfo == null){                        
105
                        IProjectInfo info = getServer().getProjectInfo();                         
106
                                this.projectInfo =  (IProjectInfo)info.clone();
107
                }
108
                this.projectInfo.setFilter(getFilter());                
109
                return this.projectInfo;
110
        }
111

    
112
        /**
113
         * 
114
         * @return the server which implements that service
115
         */
116
        public Server getServer(){
117
                return server;
118
        }        
119
        /**
120
         * 
121
         * @throws PublishException
122
         */
123
        public void publish() throws PublishException{
124
                Collection allRemoteResources = remoteResources.values();
125
                Iterator i = allRemoteResources.iterator();
126
                while(i.hasNext()){
127
                        RemoteResource r = (RemoteResource) i.next();
128
                        r.publish();
129
                }                
130
        }
131
        /**
132
         * 
133
         * @return number of remote resources in the service
134
         */
135
        public int getRemoteResourcesCount(){
136
                return remoteResources.size();
137
        }
138
        /**
139
         * 
140
         * @param position
141
         * @return the remote resource added in the position specified by the parameter
142
         */
143
        public RemoteResource getRemoteResource(int position){
144
                Collection collection = remoteResources.values();
145
                Object[] objects = collection.toArray();
146
                return (RemoteResource) objects[position];                
147
        }
148
//        /**
149
//        * Creates a remote resource for each layer
150
//        */
151
//        public void createAvailableRemoteResources(){
152
//        Set set = RemoteResourceRegister.instance().getRemoteResourcesNames(getType());
153
//        //At the moment only one remote resource by service
154
//        String remoteResourceName = (String)set.iterator().next();
155

    
156
//        //get all the layers and creates a new remote resource
157
//        ILayerInfo[] layers = view.getLayers();
158
//        for (int i=0; i< layers.length; i++){
159
//        RemoteResource r = RemoteResourceRegister.getInstance(this.getType(),remoteResourceName);
160
//        r.initialize(layers[i]);
161
//        //add the remote resource
162
//        addRemoteResource(layers[i].getName(), r);
163
//        }
164
//        }
165
        /**
166
         * TODO: I can't add two layers with the same name !!
167
         * I need an attribute name in the remote resource
168
         */
169
        public void addRemoteResource(RemoteResource r){
170
                //remoteResources.put(r.getLayerInfo().getName(), r);
171
                remoteResources.put(r.getId(), r);
172
        }
173
        /**
174
         * Useful for create a tree model
175
         *
176
        public String toString(){
177
                if(getLabel()!=null){
178
                        return getLabel();
179
                }else{
180
                        return getVersion();
181
                } 
182
        }*/
183
        /**
184
         * Adds information about the view. For each layer info, 
185
         * creates a remote resource (the first registered)
186
         * This method is only useful when one service only has one remote resource type.
187
         * If you have more one kind of remote resource, you must override this method.
188
         * 
189
         * @param view
190
         */
191
        public void addInfo(IViewInfo view) {
192
                Set set = PublishRegister.register().getRemoteResourcesNames(getServer().getRegisterTag(),getRegisterTag());
193
                //if no remote resources registered return
194
                if (set ==null){
195
                        PublishLogger.getLog().error("ERROR " + getClassName() + ":I can't add Info");
196
                        return;
197
                }
198
                //At the moment only one remote resource by service
199
                String remoteResourceName = (String)set.iterator().next();
200

    
201
                //get all the layers and creates a new remote resource
202
                ILayerInfo[] layers = view.getLayers();
203
                for (int i=0; i< layers.length; i++){
204
                        RemoteResource r = PublishRegister.register().getRemoteResource(this,remoteResourceName);
205
                        r.setLayerInfo(layers[i]);
206
                        //add the remote resource 
207
                        addRemoteResource(r);
208
                }
209

    
210
        }
211
        /**
212
         * Adds a layer information . Creates a remote resource
213
         * 
214
         * @param layer
215
         */
216
        public void addInfo(ILayerInfo layer) {
217
                Set set = PublishRegister.register().getRemoteResourcesNames(getServer().getRegisterTag(),getRegisterTag());
218
                //At the moment only one remote resource by service
219
                String remoteResourceName = (String)set.iterator().next();
220
                RemoteResource r = PublishRegister.register().getRemoteResource(this,remoteResourceName);
221
                //sets the layer info
222
                r.setLayerInfo(layer);
223
                //add the remote resource
224
                addRemoteResource(r);                                
225
        }
226

    
227
        /**
228
         * Puts all the properties into a XML in order to persist it
229
         * @return
230
         */
231
        public XMLEntity getXMLEntity() {
232
                XMLEntity xml=new XMLEntity();
233
                //put version
234
                //xml.setName(getVersion());
235
                xml.putProperty("registertag", getRegisterTag());
236
                xml.putProperty("id", getId());
237
                //put associations
238
                XMLEntity children=new XMLEntity();
239
                children.setName("remoteresources");                
240
                for (int i=0;i<getRemoteResourcesCount();i++){
241
                        children.addChild(getRemoteResource(i).getXMLEntity());
242
                }
243
                xml.addChild(children);
244
                return xml;
245
        }
246
        /**
247
         * Initializes the remote resource from the persistence
248
         * @param xml
249
         */
250
        public void setXMLEntity(XMLEntity xml) {
251
                //set properties
252
                //set children        
253
                XMLEntity children = xml.firstChild("name", "remoteresources");
254
                for (int i=0;i<children.getChildrenCount();i++){
255
                        XMLEntity xmlChild=children.getChild(i);
256
                        String remote_type = xmlChild.getStringProperty("registertag");
257
                        RemoteResource remote=PublishRegister.register().getRemoteResource(this,remote_type);
258
                        remote.setXMLEntity(xmlChild);
259
                        addRemoteResource(remote);
260
                }
261
        }
262
        /**
263
         * Removes a remote resource. It call the method removeChild() of all its remote resources
264
         * 
265
         */
266
        public void removeRemoteResource(RemoteResource r) {
267
                //If the remote resource is in the root
268
                if (remoteResources.containsKey(r.getId())){
269
                        remoteResources.remove(r.getId());
270
                }else{
271
                        for (int i = 0; i < getRemoteResourcesCount(); i++){
272
                                getRemoteResource(i).removeChild(r);
273
                        }
274
                }
275
                
276
                
277
                //notify observer
278
                setChanged();
279
                notifyObservers();
280
        }
281
        /**
282
         * Removes itself from its service
283
         */
284
        public void remove() {
285
                getServer().removeService(this);                
286
        }
287
}