Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / serverexplorer / filesystem / impl / AbstractFilesystemServerExplorerProvider.java @ 44831

History | View | Annotate | Download (6.47 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
 * 2010 {Prodevelop}   {Task}
27
 */
28
package org.gvsig.fmap.dal.serverexplorer.filesystem.impl;
29

    
30
import java.io.File;
31
import org.apache.commons.io.FilenameUtils;
32
import org.apache.commons.io.IOUtils;
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataManager;
35

    
36
import org.gvsig.fmap.dal.DataServerExplorer;
37
import org.gvsig.fmap.dal.DataStore;
38
import org.gvsig.fmap.dal.DataStoreParameters;
39
import org.gvsig.fmap.dal.NewDataStoreParameters;
40
import org.gvsig.fmap.dal.exception.CreateException;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.exception.FileNotFoundException;
43
import org.gvsig.fmap.dal.exception.RemoveException;
44
import org.gvsig.fmap.dal.resource.file.FileResource;
45
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
46
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
47
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider;
48
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory;
49
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices;
50
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
/**
55
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
56
 */
57
public abstract class AbstractFilesystemServerExplorerProvider 
58
        implements FilesystemServerExplorerProvider 
59
  {
60

    
61
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractFilesystemServerExplorerProvider.class);
62

    
63
    private String name;
64
    private String description;
65
    private FilesystemServerExplorerProviderFactory factory;
66

    
67
    protected AbstractFilesystemServerExplorerProvider() {
68
      
69
    }
70

    
71
    protected AbstractFilesystemServerExplorerProvider(FilesystemServerExplorerProviderFactory factory, String name, String description) {
72
        this.factory = factory;
73
        this.name = name;
74
        this.description = description;
75
    }
76

    
77
    protected AbstractFilesystemServerExplorerProvider(String name, String description) {
78
      this(null, name, description);
79
    }
80

    
81
    public FilesystemServerExplorerProviderFactory getFactory() {
82
      return factory;
83
    }
84
    
85
    @Override
86
    public String getDataStoreProviderName() {
87
        return this.name;
88
    }
89
    
90
    public String getName() {
91
        return this.name;
92
    }
93

    
94
    @Override
95
    public String getDescription() {
96
        return this.description;
97
    }
98
    
99
    @Override
100
    public String getResourceRootPathName(DataStore dataStore) {
101
        return this.getResourceRootPathName(dataStore.getParameters());
102
    }
103
    
104
    @Override
105
    public String getResourceRootPathName(DataStoreParameters parameters) {
106
        if( parameters instanceof FilesystemStoreParameters ) {
107
            File f = ((FilesystemStoreParameters) parameters).getFile();
108
            if( f != null ) {
109
                return FilenameUtils.removeExtension(f.getAbsolutePath());
110
            }
111
        }
112
        if( parameters.hasDynValue(FileResource.NAME) ) {
113
            Object obj = (parameters.getDynValue(FileResource.NAME));
114
            if( obj != null ) {
115
                return removeFileExtension(new File(obj.toString()));
116
            }
117
        }
118
        return null;
119
    }
120
    
121
    protected String removeFileExtension(File file) {
122
        int whereDot = file.getName().lastIndexOf(".");
123
        if( (0 < whereDot) && (whereDot <= file.getName().length() - 2) ) {
124
            whereDot = file.getAbsolutePath().lastIndexOf(".");
125
            return file.getAbsolutePath().substring(0, whereDot);
126
        }
127
        return file.getAbsolutePath();
128
    }
129

    
130
    @Override
131
    public int getMode() {
132
        return DataServerExplorer.MODE_ALL;
133
    }
134

    
135
    @Override
136
    public boolean isMode(int mode) {
137
        if( mode == DataServerExplorer.MODE_ALL ) {
138
            return true;
139
        }
140
        return (this.getMode() & mode) != 0;
141
    }
142

    
143
    public DataStoreParameters getParameters(File file) throws DataException {
144
        DataManager manager = DALLocator.getDataManager();
145
        DataStoreParameters params = manager.createStoreParameters(this.getDataStoreProviderName());
146
        ((FilesystemStoreParameters) params).setFile(file);
147
        return params;
148
    }
149

    
150
    @Override
151
    public boolean canCreate() {
152
        return false;
153
    }
154

    
155
    @Override
156
    public boolean canCreate(NewDataStoreParameters parameters) {
157
        throw new UnsupportedOperationException();
158
    }
159

    
160
    @Override
161
    public void create(NewDataStoreParameters parameters, boolean overwrite)
162
        throws CreateException {
163
        throw new UnsupportedOperationException();
164
    }
165

    
166
    @Override
167
    public NewDataStoreParameters getCreateParameters() throws DataException {
168
        throw new UnsupportedOperationException();
169
    }
170

    
171
    @Override
172
    public void remove(DataStoreParameters parameters) throws RemoveException {
173
        File file = ((FilesystemStoreParameters) parameters).getFile();
174
        if( !file.exists() ) {
175
            throw new RemoveException(this.getDataStoreProviderName(),
176
                new FileNotFoundException(file));
177
        }
178
        if( !file.delete() ) {
179
            // FIXME throws ???
180
        }
181

    
182
    }
183

    
184
    public boolean closeResourceRequested(ResourceProvider resource) {
185
        // while it is using a resource anyone can't close it
186
        return false;
187
    }
188

    
189
    public void resourceChanged(ResourceProvider resource) {
190
        //Do nothing
191

    
192
    }
193

    
194
    @Override
195
    public void initialize(FilesystemServerExplorerProviderServices serverExplorer) {
196
        
197
    }    
198
}