Revision 2195

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/resourcesstorage/FilesResourcesStorage.java
10 10
import java.net.URL;
11 11
import java.util.ArrayList;
12 12
import java.util.List;
13
import org.apache.commons.io.FileUtils;
13 14
import org.apache.commons.io.IOUtils;
14 15
import org.apache.commons.lang3.StringUtils;
15 16
import org.gvsig.tools.util.HasAFile;
......
20 21
 */
21 22
public class FilesResourcesStorage extends AbstractResourcesStorage {
22 23

  
23
    public static class FileResource 
24
    public static class FileResource
24 25
            extends AbstractResourcesStorage.AbstractResource
25
            implements ResourcesStorage.Resource, HasAFile 
26
      {
26
            implements ResourcesStorage.Resource, HasAFile {
27 27

  
28 28
        private class ResourceInputStream extends InputStream {
29 29

  
......
64 64
            super(name);
65 65
            this.resource = resource;
66 66
        }
67
        
67

  
68 68
        public String getName() {
69
          return name;
69
            return name;
70 70
        }
71 71

  
72 72
        @Override
......
140 140

  
141 141
    @Override
142 142
    public Resource getResource(String resourceName) {
143
        File fres;
144
        for (String pathName : resourcesPaths) {
145
            File folder = new File(pathName);
146
            if( folder.isDirectory() ) {
147
                fres = new File(folder,resourceName);
148
            } else {
149
                fres = new File(pathName + this.getSeparator() + resourceName);
150
            }
151
            if( fres.exists() ) {
152
                return new FileResource(resourceName, fres);
153
            }
154
        }
155
        String pathName = this.resourcesPaths.get(0);
156
        fres = new File(pathName + this.getSeparator() + resourceName);
157
        return new FileResource(resourceName, fres);
143
        return new FileResource(resourceName, getResourceFile(resourceName));
158 144
    }
159 145

  
160 146
    public List<String> getPaths() {
......
164 150
    @Override
165 151
    public List<String> getResourceNames() {
166 152
        List<String> names = new ArrayList<>();
167
        
153

  
168 154
        for (String pathName : resourcesPaths) {
169 155
            File folder = new File(pathName);
170
            if( folder.isDirectory() ) {
171
              for (File f: folder.listFiles()) {
172
                if( f.isFile() ) {
173
                  names.add(f.getName());
156
            if (folder.isDirectory()) {
157
                for (File f : folder.listFiles()) {
158
                    if (f.isFile()) {
159
                        names.add(f.getName());
160
                    }
174 161
                }
175
              }
176 162
            } else {
177
              File parent = folder.getParentFile();
178
              String prefix = pathName + this.getSeparator();
179
              int prefix_len = prefix.length();
180
              for (File f: parent.listFiles()) {
181
                if( f.isFile() ) {
182
                  String fname = f.getName();
183
                  if( StringUtils.startsWithIgnoreCase(fname, prefix) ) {
184
                    names.add(fname.substring(prefix_len));
185
                  }
163
                File parent = folder.getParentFile();
164
                String prefix = pathName + this.getSeparator();
165
                int prefix_len = prefix.length();
166
                for (File f : parent.listFiles()) {
167
                    if (f.isFile()) {
168
                        String fname = f.getName();
169
                        if (StringUtils.startsWithIgnoreCase(fname, prefix)) {
170
                            names.add(fname.substring(prefix_len));
171
                        }
172
                    }
186 173
                }
187
              }
188 174
            }
189 175
        }
190 176
        return names;
191 177
    }
192 178

  
179
    @Override
180
    public boolean remove(String resourceName) {
181
        File fileToRemove = this.getResourceFile(resourceName);
182
        return FileUtils.deleteQuietly(fileToRemove);
183
    }
184

  
185
    @Override
186
    public boolean allowRemove() {
187
        return true;
188
    }
189

  
190
    private File getResourceFile(String resourceName) {
191
        File fres;
192
        for (String pathName : resourcesPaths) {
193
            File folder = new File(pathName);
194
            if (folder.isDirectory()) {
195
                fres = new File(folder, resourceName);
196
            } else {
197
                fres = new File(pathName + this.getSeparator() + resourceName);
198
            }
199
            if (fres.exists()) {
200
                return fres;
201
            }
202
        }
203
        String pathName = this.resourcesPaths.get(0);
204
        File folder = new File(pathName);
205
        if (folder.isDirectory()) {
206
            fres = new File(folder, resourceName);
207
        } else {
208
            fres = new File(pathName + this.getSeparator() + resourceName);
209
        }
210
        return fres;
211
    }
212

  
193 213
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/resourcesstorage/AbstractResourcesStorage.java
138 138
  public List<String> getResourceNames() {
139 139
    return Collections.EMPTY_LIST;
140 140
  }
141
  
142
    @Override
143
    public boolean remove(String resourceName) {
144
       throw new UnsupportedOperationException("Not supported yet.");
145
    }
146
    
147
    @Override
148
    public boolean allowRemove() {
149
        return false;
150
    }
141 151

  
142 152
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/resourcesstorage/BaseMultiResourcesStorage.java
103 103
        return names;
104 104
    }
105 105
    
106
    @Override
107
    public boolean remove(String resourceName) {
108
        throw new UnsupportedOperationException("Not supported yet.");
109
    }
110

  
111
    @Override
112
    public boolean allowRemove() {
113
        return false;
114
    }
115
    
106 116
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/resourcesstorage/EmptyResourcesStorage.java
96 96
      return Collections.EMPTY_LIST;
97 97
    }
98 98

  
99
    @Override
100
    public boolean remove(String resourceName) {
101
        throw new UnsupportedOperationException("Not supported yet.");
102
    }
103

  
104
    @Override
105
    public boolean allowRemove() {
106
        return false;
107
    }
99 108
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/resourcesstorage/ResourcesStorage.java
89 89
    public boolean isReadOnly();
90 90
    
91 91
    public boolean exists(String resourceName);
92
    
93
    public boolean remove(String resourceName);
94
    
95
    public boolean allowRemove();
92 96

  
93 97
    /**
94 98
     * Return the resource names in this store.
......
97 101
     * @return list of resource names or empty list.
98 102
     */
99 103
    public List<String> getResourceNames();
100

  
104
    
101 105
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/bookmarksandhistory/impl/DefaultBookmarksAndHistoryManager.java
4 4
import java.io.IOException;
5 5
import java.util.Collection;
6 6
import java.util.HashMap;
7
import java.util.HashSet;
7 8
import java.util.List;
8 9
import java.util.Map;
9 10
import org.apache.commons.lang3.StringUtils;
......
15 16
import org.gvsig.tools.persistence.PersistenceManager;
16 17
import org.gvsig.tools.persistence.PersistentState;
17 18
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
18 21

  
19 22
/**
20 23
 *
......
26 29
    private final Map<String, History<Object>> historyGroups;
27 30
    private ResourcesStorage primaryStorage;
28 31
    private ResourcesStorage secondaryStorage;
29
    private final int DEFAULT_VALUE_HISTORY_SIZE = 30;
32
    private final int DEFAULT_VALUE_HISTORY_SIZE = 20;
33
    private final Logger LOGGER = LoggerFactory.getLogger(DefaultBookmarksAndHistoryManager.class);
30 34

  
31 35
    public DefaultBookmarksAndHistoryManager() {
32 36
        this.bookmarkGroups = new HashMap<>();
......
113 117
    public void save() {
114 118
        if (this.primaryStorage != null) {
115 119
            saveBookmarks(primaryStorage);
116
            saveHitory(primaryStorage);
120
            saveHistory(primaryStorage);
117 121
        }
118 122
        if (this.secondaryStorage != null) {
119 123
            saveBookmarks(secondaryStorage);
120
            saveHitory(secondaryStorage);
124
            saveHistory(secondaryStorage);
121 125
        }
122 126
    }
123 127

  
......
125 129
        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
126 130
        SimpleIdentityManager userManager = ToolsLocator.getIdentityManager();
127 131
        String userName = userManager.getCurrentIdentity().getID();
132
        HashSet<String> addedResources = new HashSet<String>();
128 133
        for (Map.Entry<String, Bookmarks<Object>> bookmarksEntry : this.bookmarkGroups.entrySet()) {
129 134
            String bookmarksGroupName = bookmarksEntry.getKey();
130 135
            Bookmarks<Object> bookmarks = bookmarksEntry.getValue();
131 136
            for (Bookmark<Object> bookmark : bookmarks) {
132 137
                String resourceName;
133 138
                if (bookmark.isGlobal()) {
139
                    resourceName = "ALL@BOOKMARKS!" + bookmarksGroupName + "!" + bookmark.getName();
140
                } else {
134 141
                    resourceName = userName + "@BOOKMARKS!" + bookmarksGroupName + "!" + bookmark.getName();
135
                } else {
136
                    resourceName = "ALL@BOOKMARKS!" + bookmarksGroupName + "!" + bookmark.getName();
137 142
                }
138 143
                try (ResourcesStorage.Resource resource = storage.getResource(resourceName)) {
139 144
                    Object data = bookmark.getValue();
140 145
                    PersistentState state = persistenceManager.getState(data);
141 146
                    persistenceManager.saveState(state, resource.asOutputStream());
147
                    addedResources.add(resourceName);
142 148
                } catch (Exception ex) {
143 149
                    // ???
144 150
                }
145 151
            }
146 152
        }
153

  
154
        if (storage.allowRemove() && !addedResources.isEmpty()) {
155
            for (String resourceName : storage.getResourceNames()) {
156
                if (resourceName.contains(userName + "@BOOKMARKS!")) {
157
                    if (!addedResources.contains(resourceName)) {
158
                        storage.remove(resourceName);
159
                    }
160
                }
161

  
162
            }
163
        }
147 164
    }
148 165

  
149 166
    @Override
......
165 182

  
166 183
        List<String> names = storage.getResourceNames();
167 184
        for (String name : names) {
168
            String[] userAndBookmark = StringUtils.split(name, "@");
169
            String currentUserName = userAndBookmark[0];
170
            String[] groupAndId = StringUtils.split(userAndBookmark[1], "!");
171
            if (groupAndId.length != 3) {
172
                continue;
173
            }
174
            String typeName = groupAndId[0];
175
            if (!StringUtils.equalsIgnoreCase(typeName, "BOOKMARKS")) {
176
                continue;
177
            }
178
            String bookmarksGroupName = groupAndId[1];
179
            String bookmarkName = groupAndId[2];
185
            try {
186
                String[] userAndBookmark = StringUtils.split(name, "@");
187
                String currentUserName = userAndBookmark[0];
188
                String[] groupAndId = StringUtils.split(userAndBookmark[1], "!");
189
                if (groupAndId.length != 3) {
190
                    continue;
191
                }
192
                String typeName = groupAndId[0];
193
                if (!StringUtils.equalsIgnoreCase(typeName, "BOOKMARKS")) {
194
                    continue;
195
                }
196
                String bookmarksGroupName = groupAndId[1];
197
                String bookmarkName = groupAndId[2];
180 198

  
181
            if (StringUtils.equalsIgnoreCase(currentUserName, userName)) {
182
                try (ResourcesStorage.Resource resource = storage.getResource(name)) {
183
                    Object data = persistenceManager.getObject(resource.asInputStream());
184
                    Bookmarks<Object> bookmarks = this.bookmarkGroups.get(bookmarksGroupName);
185
                    bookmarks.add(bookmarkName, data);
186
                } catch (IOException ex) {
199
                if (StringUtils.equalsIgnoreCase(currentUserName, userName)) {
200
                    try (ResourcesStorage.Resource resource = storage.getResource(name)) {
201
                        Object data = persistenceManager.getObject(resource.asInputStream());
202
                        Bookmarks<Object> bookmarks = this.getBookmarksGroup(bookmarksGroupName);
203
                        bookmarks.add(bookmarkName, data);
204
                    } catch (IOException ex) {
187 205

  
188
                }
189
            } else if (StringUtils.equalsIgnoreCase(currentUserName, "ALL")) {
190
                try (ResourcesStorage.Resource resource = storage.getResource(name)) {
191
                    Object data = persistenceManager.getObject(resource.asInputStream());
192
                    Bookmarks<Object> bookmarks = this.bookmarkGroups.get(bookmarksGroupName);
193
                    Bookmark bookmark = bookmarks.add(bookmarkName, data);
194
                    bookmark.setGlobal(true);
195
                } catch (IOException ex) {
206
                    }
207
                } else if (StringUtils.equalsIgnoreCase(currentUserName, "ALL")) {
208
                    try (ResourcesStorage.Resource resource = storage.getResource(name)) {
209
                        Object data = persistenceManager.getObject(resource.asInputStream());
210
                        Bookmarks<Object> bookmarks = this.getBookmarksGroup(bookmarksGroupName);
211
                        Bookmark bookmark = bookmarks.add(bookmarkName, data);
212
                        bookmark.setGlobal(true);
213
                    } catch (IOException ex) {
196 214

  
215
                    }
197 216
                }
217
            } catch (Exception ex) {
218
                continue;
198 219
            }
199 220
        }
200 221
    }
201 222

  
202
    private void saveHitory(ResourcesStorage storage) {
223
    private void saveHistory(ResourcesStorage storage) {
203 224
        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
204 225
        SimpleIdentityManager userManager = ToolsLocator.getIdentityManager();
205 226
        String userName = userManager.getCurrentIdentity().getID();
......
214 235
                    PersistentState state = persistenceManager.getState(data);
215 236
                    persistenceManager.saveState(state, resource.asOutputStream());
216 237
                } catch (Exception ex) {
217
                    // ???
238
                    LOGGER.warn("Fail saveing state from history record:" + resourceName, ex);
218 239
                }
219 240
            }
220 241
        }
......
227 248

  
228 249
        List<String> names = storage.getResourceNames();
229 250
        for (String name : names) {
230
            String[] userAndHistory = StringUtils.split(name, "@");
231
            String currentUserName = userAndHistory[0];
232
            String[] groupAndId = StringUtils.split(userAndHistory[1], "!");
233
            if (groupAndId.length != 3) {
234
                continue;
235
            }
236
            String typeName = groupAndId[0];
237
            if (!StringUtils.equalsIgnoreCase(typeName, "HISTORY")) {
238
                continue;
239
            }
240
            String groupName = groupAndId[1];
251
            try {
252
                String[] userAndHistory = StringUtils.split(name, "@");
253
                String currentUserName = userAndHistory[0];
254
                String[] groupAndId = StringUtils.split(userAndHistory[1], "!");
255
                if (groupAndId.length != 3) {
256
                    continue;
257
                }
258
                String typeName = groupAndId[0];
259
                if (!StringUtils.equalsIgnoreCase(typeName, "HISTORY")) {
260
                    continue;
261
                }
262
                String groupName = groupAndId[1];
241 263

  
242
            if (StringUtils.equalsIgnoreCase(currentUserName, userName)) {
243
                try (ResourcesStorage.Resource resource = storage.getResource(name)) {
244
                    Object data = persistenceManager.getObject(resource.asInputStream());
245
                    History<Object> history = this.historyGroups.get(groupName);
246
                    history.add(data);
247
                } catch (IOException ex) {
248

  
264
                if (StringUtils.equalsIgnoreCase(currentUserName, userName)) {
265
                    try (ResourcesStorage.Resource resource = storage.getResource(name)) {
266
                        Object data = persistenceManager.getObject(resource.asInputStream());
267
                        History<Object> history = this.getHistoryGroup(groupName);
268
                        history.add(data);
269
                    } catch (IOException ex) {
270
                        LOGGER.warn("Load history fail: " + name, ex);
271
                    }
249 272
                }
273
            } catch (Exception ex) {
274
                LOGGER.warn("*Load history resources fail: " + name, ex);
275
                continue;
250 276
            }
251 277
        }
252 278
    }

Also available in: Unified diff