Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / resource / spi / MultiResource.java @ 40435

History | View | Annotate | Download (7.4 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {}  {{Task}}
26
 */
27
package org.gvsig.fmap.dal.resource.spi;
28

    
29
import java.util.ArrayList;
30
import java.util.List;
31

    
32
import org.gvsig.fmap.dal.DALLocator;
33
import org.gvsig.fmap.dal.exception.InitializeException;
34
import org.gvsig.fmap.dal.resource.ResourceParameters;
35
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
36
import org.gvsig.fmap.dal.resource.exception.PrepareResourceException;
37
import org.gvsig.fmap.dal.resource.exception.ResourceException;
38
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException;
39
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException;
40
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyDisposeException;
41
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
/**
46
 * Resource implementation which is able to show an unique interface to a group
47
 * of Resources.
48
 * 
49
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
50
 */
51
public class MultiResource extends AbstractResource {
52

    
53
        public static final String TYPE_NAME = "multi";
54

    
55
        public static final String DESCRIPTION = "Group of multiple resources";
56

    
57
        private static final Logger LOG =
58
                        LoggerFactory.getLogger(MultiResource.class);
59

    
60
        private List resources = new ArrayList();
61

    
62
        // Initially, the first one
63
        private int defaultResourcePos = 0;
64

    
65
        /**
66
         * Creates a new {@link MultiResource} from the given parameters.
67
         * 
68
         * @param parameters
69
         *            to create the resource from
70
         * @throws InitializeException
71
         *             if there is an error creating the resource
72
         */
73
        public MultiResource(MultiResourceParameters parameters)
74
                        throws InitializeException {
75
                super(parameters);
76
        }
77

    
78
        private List getResources() {
79
                return resources;
80
        }
81

    
82
        /**
83
         * Adds a new {@link ResourceProvider} to the list of Resources managed by
84
         * this group.
85
         * 
86
         * @param parameters
87
         *            to create the resource from
88
         * @param defaultResource
89
         *            if the added resource is to be used as the multi resource
90
         *            default one
91
         * @throws InitializeException
92
         *             if there is an error adding the resource
93
         */
94
        public void addResource(ResourceParameters parameters,
95
                        boolean defaultResource) throws InitializeException {
96
                ResourceProvider resourceProvider =
97
                                ((ResourceManagerProviderServices) DALLocator.getResourceManager()).createResource(parameters);
98
                resources.add(resourceProvider);
99
                if (defaultResource) {
100
                        defaultResourcePos = resources.size() - 1;
101
                }
102
        }
103

    
104
        /**
105
         * Adds a new {@link ResourceProvider} to the list of Resources managed by
106
         * this group.
107
         * 
108
         * @param name
109
         *            of the resource to add
110
         * @param params
111
         *            parameters to create the resource with
112
         * @param defaultResource
113
         *            if the added resource is to be used as the multi resource
114
         *            default one
115
         * @throws InitializeException
116
         *             if there is an error adding the resource
117
         */
118
        public void addResource(String name, Object[] params,
119
                        boolean defaultResource) throws InitializeException {
120
                ResourceProvider resourceProvider =
121
                                ((ResourceManagerProviderServices) DALLocator.getResourceManager()).createResource(
122
                                                name, params);
123
                resources.add(resourceProvider);
124
                if (defaultResource) {
125
                        defaultResourcePos = resources.size() - 1;
126
                }
127
        }
128

    
129
        public boolean isThis(ResourceParameters parameters)
130
                        throws ResourceException {
131
                if (parameters instanceof MultiResourceParameters) {
132
                        MultiResourceParameters multiParams =
133
                                        (MultiResourceParameters) parameters;
134
                        return multiParams.getName() != null
135
                                        && multiParams.getName().equals(
136
                                                        getMultiResourceParameters().getName());
137
                }
138

    
139
                return false;
140
        }
141

    
142
        public void notifyChanges() throws ResourceNotifyChangesException {
143
                List resources = getResources();
144
                for (int i = 0; i < resources.size(); i++) {
145
                        ((ResourceProvider) getResources().get(i)).notifyChanges();
146
                }
147
        }
148

    
149
        public void notifyClose() throws ResourceNotifyCloseException {
150
                List resources = getResources();
151
                for (int i = 0; i < resources.size(); i++) {
152
                        ((ResourceProvider) getResources().get(i)).notifyClose();
153
                }
154
        }
155

    
156
        public void notifyDispose() throws ResourceNotifyDisposeException {
157
                List resources = getResources();
158
                for (int i = 0; i < resources.size(); i++) {
159
                        ((ResourceProvider) getResources().get(i)).notifyDispose();
160
                }
161
        }
162

    
163
        public void notifyOpen() throws ResourceNotifyOpenException {
164
                List resources = getResources();
165
                for (int i = 0; i < resources.size(); i++) {
166
                        ((ResourceProvider) getResources().get(i)).notifyOpen();
167
                }
168
        }
169

    
170
        public void prepare() throws PrepareResourceException {
171
                List resources = getResources();
172
                for (int i = 0; i < resources.size(); i++) {
173
                        ((ResourceProvider) getResources().get(i)).prepare();
174
                }
175
        }
176

    
177
        public void closeRequest() throws ResourceException {
178
                List resources = getResources();
179
                for (int i = 0; i < resources.size(); i++) {
180
                        ((ResourceProvider) getResources().get(i)).closeRequest();
181
                }
182
        }
183

    
184
        public Object get() throws AccessResourceException {
185
                ResourceProvider defaultResource = getDefaultResource();
186
                if (defaultResource == null) {
187
                        return null;
188
                } else {
189
                        return defaultResource.get();
190
                }
191
        }
192

    
193
        public Object getData() {
194
                ResourceProvider defaultResource = getDefaultResource();
195
                if (defaultResource == null) {
196
                        return null;
197
                } else {
198
                        return defaultResource.getData();
199
                }
200
        }
201

    
202
        private MultiResourceParameters getMultiResourceParameters() {
203
                return (MultiResourceParameters) getParameters();
204
        }
205

    
206
        public String getName() throws AccessResourceException {
207
                StringBuffer buffer =
208
                                new StringBuffer().append("MultiResource ").append(
209
                                                getMultiResourceParameters().getName()).append(":: ");
210
                List resources = getResources();
211
                for (int i = 0; i < resources.size(); i++) {
212
                        buffer.append(((ResourceProvider) resources.get(i)).getName());
213
                        if (defaultResourcePos == i) {
214
                                buffer.append(" (default)");
215
                        }
216
                        if (i < resources.size() - 1) {
217
                                buffer.append(", ");
218
                        }
219
                }
220
                return buffer.toString();
221
        }
222

    
223
        public String toString() {
224
                try {
225
                        return getName();
226
                } catch (AccessResourceException e) {
227
                        LOG.error("Error getting the resource name", e);
228
                }
229
                return super.toString();
230
        }
231

    
232
        private ResourceProvider getDefaultResource() {
233
                if (getResources().size() > 0) {
234
                        return (ResourceProvider) getResources().get(defaultResourcePos);
235
                } else {
236
                        return null;
237
                }
238
        }
239

    
240
        public void setData(Object data) {
241
                ResourceProvider defaultResource = getDefaultResource();
242
                if (defaultResource != null) {
243
                        defaultResource.setData(data);
244
                }
245
        }
246
}