Statistics
| Revision:

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

History | View | Annotate | Download (6.62 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.Set;
47

    
48
import org.gvsig.publish.IPublishPersistence;
49
import org.gvsig.publish.PublishLogger;
50
import org.gvsig.publish.PublishRegister;
51
import org.gvsig.publish.exceptions.PublishException;
52
import org.gvsig.publish.infoproject.ILayerInfo;
53

    
54
import com.iver.utiles.XMLEntity;
55

    
56
/**
57
 * This class represents a remote resource
58
 * 
59
 * @author Jos? Vicente Hig?n (josevicente.higon@iver.es)
60
 *
61
 */
62
public abstract class RemoteResource implements IPublishPersistence {
63
        //associations
64
        private LinkedHashMap childs = null;
65
        protected ILayerInfo layerInfo = null;
66
        private Service service=null;
67
        //attributes
68
        private String id = null;
69

    
70
        /**
71
         * Constructor
72
         * 
73
         * Only creates a empty hashMap for its childs 
74
         *  
75
         */
76
        public RemoteResource(Service service){
77
                childs = new LinkedHashMap();
78
                this.service = service;
79
        }
80
        /**
81
         * 
82
         * @return unique identifier which identifies the remote resource, It's the layerinfo name 
83
         * 
84
         */
85
        public String getId(){
86
                if (id == null){                        
87
                                this.id = getLayerInfo().getName();                        
88
                }
89
                return id;
90
        }
91
        private void setId(String id){
92
                this.id = id;
93
        }
94
        /**
95
         * 
96
         * @return string to register 
97
         */
98
        public abstract String getRegisterTag();
99

    
100
        /**
101
         * 
102
         * @return the service which serves this remote resource
103
         */
104
        public Service getService(){
105
                return this.service;
106
        }
107
        /**
108
         * Preconditions: exists information about the layer. The availability of the information is checked when a 
109
         * project information is set to the publication 
110
         * 
111
         * @return the layer which represents this remote resource. I can be null if the remote resource has not a layerinfo associated.
112
         *   
113
         */
114
        public ILayerInfo getLayerInfo() {
115
                if (layerInfo == null){
116
                        layerInfo = getService().getServer().getPublication().getProjectInfo().findLayer(getId());                        
117
                }
118
                return this.layerInfo;
119
        }
120
        /**
121
         * 
122
         * @return number of remote resources childs
123
         */
124
        public int getChildsCount(){
125
                return childs.size();
126
        }
127

    
128
        /**
129
         * 
130
         * @param position
131
         * @return the remote resource added in the position specified by the param
132
         */
133
        public RemoteResource getChild(int position){
134
                Collection collection = childs.values();
135
                Object[] objects = collection.toArray();
136
                return (RemoteResource) objects[position];                
137
        }
138
        /**
139
         * Add a remoteresource child
140
         * @param r
141
         */
142
        public void addChild(RemoteResource r){
143
                childs.put(r.getId(), r);
144
        }
145
        /**
146
         * Removes itself from its service
147
         */
148
        public void remove(){
149

    
150
                getService().removeRemoteResource(this);
151
        }
152
        /**
153
         * Call the method publish of each child
154
         * @throws PublishException
155
         * @throws LayerInfoNotAvailableException 
156
         */
157
        public void publish() throws PublishException{
158
                //Call childs publish method 
159
                Collection allRemoteResources = childs.values();
160
                Iterator i = allRemoteResources.iterator();
161
                while(i.hasNext()){
162
                        RemoteResource r = (RemoteResource) i.next();
163
                        r.publish();
164
                }                
165
        }
166

    
167
        /**
168
         * Initializes the remote resource with a layer info.
169
         * Creates a new remote resource child for each layerinfo child
170
         * 
171
         * @param layerInfo
172
         */
173

    
174
        public void setLayerInfo(ILayerInfo ilayer) {
175
                //TODO: clear all its childs?
176
                //childs.clear();
177
                //assign the reference to infolayer
178
                layerInfo = ilayer;
179
                //creates its childs
180
                ILayerInfo[] layerInfos = layerInfo.getChilds();
181
                if (layerInfos != null){
182
                        for (int i = 0; i < layerInfos.length; i++){                        
183
                                RemoteResource child = null;
184
                                Set set = PublishRegister.register().getRemoteResourcesNames(getService().getServer().getRegisterTag(),getService().getRegisterTag());
185
                                //At the moment only one remote resource by service
186
                                String remoteResourceName = (String)set.iterator().next();
187
                                child = PublishRegister.register().getRemoteResource(getService(),remoteResourceName);                                
188
                                //TODO: check problems in initialize
189
                                child.setLayerInfo(layerInfos[i]);
190
                                childs.put(layerInfos[i].getName(), child);
191
                        }
192
                } 
193
        }
194

    
195
        /**
196
         * Puts all the properties into a XML in order to persist it
197
         * 
198
         * @return
199
         */
200
        public XMLEntity getXMLEntity() {
201
                XMLEntity xml=new XMLEntity();
202
                //put the version?? NO because is abstract
203
                //put properties
204
                xml.putProperty("registertag", getRegisterTag());
205
                xml.putProperty("id", getId());
206
                //put associations                 
207
                XMLEntity xmlRemotes = new XMLEntity();
208
                xmlRemotes.setName("children");
209
                for (int i = 0; i< getChildsCount(); i++){
210
                        RemoteResource child = getChild(i);
211
                        xmlRemotes.addChild(child.getXMLEntity());
212
                }
213
                xml.addChild(xmlRemotes);
214
                return xml;
215
        }
216
        /**
217
         * Initializes the remote resource from the persistence
218
         * @param xml
219
         */
220
        public void setXMLEntity(XMLEntity xml) {                                        
221
                setId(xml.getStringProperty("id"));
222
                XMLEntity children = xml.firstChild("name", "children");
223
                for (int i=0; i<children.getChildrenCount(); i++){
224
                        XMLEntity child = children.getChild(i);
225
                        RemoteResource r = PublishRegister.register().getRemoteResource(getService(), child.getStringProperty("registertag"));
226
                        if (r == null){
227
                                PublishLogger.getLog().error("ERROR RemoteResource: I can't create a child from persistence");
228
                        }
229
                        r.setXMLEntity(child);
230
                        addChild(r);
231
                }
232

    
233
        }
234
        /**
235
         * Removes the child if exists 
236
         * 
237
         * @param r remote resource to remove
238
         */
239
        public void removeChild(RemoteResource r) {
240

    
241
                if (childs.containsKey(r.getId())){
242
                        childs.remove(r.getId());
243
                        if (childs.isEmpty()){
244
                                remove();
245
                        }
246
                }else{
247
                        for (int i = 0; i < getChildsCount(); i++){
248
                                getChild(i).removeChild(r);
249
                        }
250
                }
251

    
252
        }
253

    
254
}