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 @ 40559

History | View | Annotate | Download (7.45 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2009 {}  {{Task}}
27
 */
28
package org.gvsig.fmap.dal.resource.spi;
29

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

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

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

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

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

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

    
61
        private List resources = new ArrayList();
62

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

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

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

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

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

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

    
140
                return false;
141
        }
142

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

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

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

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

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

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

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

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

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

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

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

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

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