Statistics
| Revision:

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

History | View | Annotate | Download (6.19 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.infoproject.ILayerInfo;
52
import org.gvsig.publish.infoproject.ProjectInfoFactory;
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
        //public static final String REMOTERESOURCE_TYPE_KEY = "Type";
64
        
65

    
66
        /*
67
         * Hashmap with its childs
68
         */
69
        private LinkedHashMap childs = null;
70

    
71
        /*
72
         * Reference to layerinfo
73
         */
74
        protected ILayerInfo layerInfo = null;
75
        /*
76
         * Reference to service
77
         * 
78
         */
79
        private Service service=null;
80
        
81
        /**
82
         * Constructor
83
         * 
84
         * Only creates a empty hashMap for its childs 
85
         *  
86
         */
87
        public RemoteResource(Service service){
88
                childs = new LinkedHashMap();
89
                this.service = service;
90
        }
91
        /**
92
         * 
93
         * @return unique identifier which identifies a service instance 
94
         */
95
        public abstract String getId();
96
        /**
97
         * 
98
         * @return string to register 
99
         */
100
        public abstract String getRegisterTag();
101

    
102
        /**
103
         * 
104
         * @return the service which serves this remote resource
105
         */
106
        public Service getService(){
107
                return this.service;
108
        }
109
        /**
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
                return this.layerInfo;
116
        }
117
        /**
118
         * 
119
         * @return number of remote resources childs
120
         */
121
        public int getChildsCount(){
122
                return childs.size();
123
        }
124

    
125
        /**
126
         * 
127
         * @param position
128
         * @return the remote resource added in the position specified by the param
129
         */
130
        public RemoteResource getChild(int position){
131
                Collection collection = childs.values();
132
                Object[] objects = collection.toArray();
133
                return (RemoteResource) objects[position];                
134
        }
135
        /**
136
         * Add a remoteresource child
137
         * @param r
138
         */
139
        public void addChild(RemoteResource r){
140
                childs.put(r.getId(), r);
141
        }
142
        /**
143
         * Removes itself from its service
144
         */
145
        public void remove(){
146
                getService().removeRemoteResource(this);
147
        }
148
        /**
149
         * Call the method publish of each child
150
         * @throws PublishException
151
         */
152
        public void publish() throws PublishException{
153
                //Call childs publish method 
154
                Collection allRemoteResources = childs.values();
155
                Iterator i = allRemoteResources.iterator();
156
                while(i.hasNext()){
157
                        RemoteResource r = (RemoteResource) i.next();
158
                        r.publish();
159
                }                
160
        }
161

    
162
        /**
163
         * Initializes the remote resource with a layer info.
164
         * Creates a new remote resource child for each layerinfo child
165
         * 
166
         * @param layerInfo
167
         */
168

    
169
        public void setLayerInfo(ILayerInfo ilayer) {
170
                //TODO: clear all its childs?
171
                //childs.clear();
172
                //assign the reference to infolayer
173
                layerInfo = ilayer;
174
                //creates its childs
175
                ILayerInfo[] layerInfos = layerInfo.getChilds();
176
                if (layerInfos != null){
177
                        for (int i = 0; i < layerInfos.length; i++){                        
178
                                RemoteResource child = null;
179
                                Set set = PublishRegister.register().getRemoteResourcesNames(getService().getServer().getRegisterTag(),getService().getRegisterTag());
180
                                //At the moment only one remote resource by service
181
                                String remoteResourceName = (String)set.iterator().next();
182
                                child = PublishRegister.register().getRemoteResource(getService(),remoteResourceName);                                
183
                                //TODO: check problems in initialize
184
                                child.setLayerInfo(layerInfos[i]);
185
                                childs.put(layerInfos[i].getName(), child);
186
                        }
187
                } 
188
        }
189
        
190
        /**
191
         * Puts all the properties into a XML in order to persist it
192
         * 
193
         * @return
194
         */
195
        public XMLEntity getXMLEntity() {
196
                XMLEntity xml=new XMLEntity();
197
                //put the version??
198
                //put properties
199
                xml.putProperty("registertag", getRegisterTag());
200
                xml.putProperty("id", getId());
201
                //put associations                 
202
                XMLEntity xmlLayer = new XMLEntity();
203
                xmlLayer.setName("layerinfo");
204
                xmlLayer.addChild(getLayerInfo().getXMLEntity());
205
                xml.addChild(xmlLayer);
206
                XMLEntity xmlRemotes = new XMLEntity();
207
                xmlRemotes.setName("children");
208
                for (int i = 0; i< getChildsCount(); i++){
209
                        RemoteResource child = getChild(i);
210
                        xmlRemotes.addChild(child.getXMLEntity());
211
                }
212
                xml.addChild(xmlRemotes);
213
                return xml;
214
        }
215
        /**
216
         * Initializes the remote resource from the persistence
217
         * @param xml
218
         */
219
        public void setXMLEntity(XMLEntity xml) {                        
220
                //set associations                                 
221
                XMLEntity xmlLayerInfo = xml.firstChild("name","layerinfo");
222
                Object o = ProjectInfoFactory.createFromXML(xmlLayerInfo.getChild(0));
223
                layerInfo = (ILayerInfo)o;                                                 
224
                
225
                XMLEntity children = xml.firstChild("name", "children");
226
                for (int i=0; i<children.getChildrenCount(); i++){
227
                        XMLEntity child = children.getChild(i);
228
                        RemoteResource r = PublishRegister.register().getRemoteResource(getService(), child.getName());
229
                        if (r == null){
230
                                PublishLogger.getLog().error("ERROR RemoteResource: I can't create a child from persistence");
231
                        }
232
                        r.setXMLEntity(child);
233
                        addChild(r);
234
                }
235

    
236
        }
237

    
238
}