001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.rGUI.server.controllers;
020
021//import com.sun.tools.javac.util.DefaultFileManager.ZipArchive;
022import pt.ua.dicoogle.server.ControlServices;
023import java.io.File;
024import java.net.URI;
025import java.net.URISyntaxException;
026import java.rmi.RemoteException;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import pt.ua.dicoogle.core.ServerSettings;
030import pt.ua.dicoogle.plugins.PluginController;
031import pt.ua.dicoogle.rGUI.interfaces.controllers.IDirectory;
032import pt.ua.dicoogle.server.DicomDirCreator;
033
034/**
035 * Controller of Directory Settings
036 *
037 * @author Samuel Campos <samuelcampos@ua.pt>
038 */
039@Deprecated
040public class DirectorySettings implements IDirectory {
041
042    private static final Logger logger = LoggerFactory.getLogger(DirectorySettings.class);
043    
044    private String storagePath;
045    private String dicoogleDir;
046    private int effort;
047    private boolean saveTumbnails;
048    private String thumbnailsMatrix;
049    private boolean indezZip;
050    private boolean gzipStorage;
051    private boolean indexAnonymous;
052    private boolean monitorWatcher;
053    
054
055    private ServerSettings settings;
056    
057    private static DirectorySettings instance ;
058
059    public static synchronized DirectorySettings getInstance()
060    {
061
062        if (instance == null) 
063            instance = new DirectorySettings();
064
065        return instance;
066    }
067
068    private DirectorySettings()
069    {
070        settings = ServerSettings.getInstance();
071
072        loadSettings();
073    }
074
075    /**
076     * Load settings from ServerSettings
077     */
078    public void loadSettings(){
079        storagePath = settings.getPath();
080        dicoogleDir = settings.getDicoogleDir();
081        effort = settings.getIndexerEffort();
082        saveTumbnails = settings.getSaveThumbnails();
083        thumbnailsMatrix = settings.getThumbnailsMatrix();
084        indezZip = settings.isIndexZIPFiles();
085        gzipStorage = settings.isGzipStorage();
086        monitorWatcher = settings.isMonitorWatcher();
087        this.indexAnonymous = settings.isIndexAnonymous();
088    }
089
090    /**
091     * Save settings to ServerSettings
092     */
093    public void saveSettings(){
094        settings.setPath(storagePath);
095        settings.setDicoogleDir(dicoogleDir);
096        settings.setIndexerEffort(effort);
097        settings.setSaveThumbnails(saveTumbnails);
098        settings.setThumbnailsMatrix(thumbnailsMatrix);
099        settings.setIndexZIPFiles(saveTumbnails);
100        settings.setIndexZIPFiles(indezZip);
101        settings.setGzipStorage(gzipStorage);
102        settings.setMonitorWatcher(isMonitorWatcher());
103        settings.setIndexAnonymous(indexAnonymous);
104    }
105
106
107    /**
108     *
109     * @return  true - if there are unsaved settings ( != ServerSettings)
110     *          false - not
111     */
112    public boolean unsavedSettings(){
113        
114        if(!storagePath.equals(settings.getPath()) || !dicoogleDir.equals(settings.getDicoogleDir()) ||
115                effort != settings.getIndexerEffort() || saveTumbnails != settings.getSaveThumbnails() ||
116                monitorWatcher != settings.isMonitorWatcher() || 
117                indezZip != settings.isIndexZIPFiles() || 
118                indexAnonymous != settings.isIndexAnonymous() || 
119                !thumbnailsMatrix.equals(settings.getThumbnailsMatrix()))
120            return true;
121
122        return false;
123    }
124
125    /**
126     *
127     * @param path
128     * @return  0 - Ok
129     *          1 - Not a directory
130     *          2 - Can't read
131     *          3 - Can't write
132     * @throws RemoteException
133     */
134    @Override
135    public int setStoragePath(String path) throws RemoteException {
136        File file = new File(path);
137
138        if(!file.isDirectory())
139            return 1;
140
141        if(!file.canRead())
142            return 2;
143
144        if(!file.canWrite())
145            return 3;
146
147        storagePath = path;
148        
149        return 0;
150    }
151
152    @Override
153    public String getStoragePath() throws RemoteException {
154        return storagePath;
155    }
156
157    @Override
158    public int setDicoogleDir(String path) throws RemoteException {
159        File file = new File(path);
160
161        if(!file.isDirectory())
162            return 1;
163
164        if(!file.canRead())
165            return 2;
166
167        if(!file.canWrite())
168            return 3;
169
170        dicoogleDir = path;
171
172        return 0;
173    }
174
175    @Override
176    public String getDicoogleDir() throws RemoteException {
177        return dicoogleDir;
178    }
179
180    @Override
181    public boolean setIndexerEffort(int effort) throws RemoteException {
182        if(effort < 0 || effort > 100)
183            return false;
184
185        this.effort = effort;
186        
187        return true;
188    }
189
190    @Override
191    public int getIndexerEffort() throws RemoteException {
192        return effort;
193    }
194
195    @Override
196    public void setSaveThumbnails(boolean value) throws RemoteException {
197        saveTumbnails = value;
198    }
199
200    @Override
201    public boolean getSaveThumbnails() throws RemoteException {
202        return saveTumbnails;
203    }
204
205    @Override
206    public void setThumbnailsMatrix(String ThumbnailsMatrix) throws RemoteException {
207        thumbnailsMatrix = ThumbnailsMatrix;
208    }
209
210    @Override
211    public String getThumbnailsMatrix() throws RemoteException {
212        return thumbnailsMatrix;
213    }
214
215
216    /**
217     *
218     * @return  0 - OK
219     *          1 - Defined storage path is invalid
220     *          2 - Server running
221     * @throws RemoteException
222     */
223    @Override
224    public int rebuildIndex() throws RemoteException {
225        saveSettings();
226        
227        ControlServices serv = ControlServices.getInstance();
228
229        if (!serv.storageIsRunning()) {
230            File f = new File(storagePath);
231            //DebugManager.getInstance().debug("Rebuild Search Index on: " + f.getAbsolutePath());
232            
233            if (!f.exists()) {
234                return 1;
235            }
236
237            try {
238                PluginController.getInstance().index(new URI(f.getAbsolutePath()));
239            } catch (URISyntaxException ex) {
240                logger.error(ex.getMessage(), ex);
241            }
242            
243        } else
244            return 2;
245
246        return 0;
247    }
248
249
250    /**
251     *
252     * @return  0 - OK
253     *          1 - Defined storage path is invalid
254     *          2 - Server running
255     * @throws RemoteException
256     */
257    @Override
258    public int rebuildDICOMDir() throws RemoteException {
259        saveSettings();
260
261        ControlServices serv = ControlServices.getInstance();
262
263        if (!serv.storageIsRunning()) {
264            File f = new File(dicoogleDir);
265            //DebugManager.getInstance().debug(f.getAbsolutePath());
266
267            if (!f.exists()) {
268                return 1;
269            }
270
271            DicomDirCreator dirc = new DicomDirCreator(dicoogleDir, "Dicoogle");
272            dirc.dicomdir_rebuild();
273        } else
274            return 2;
275
276        return 0;
277    }
278
279    @Override
280    public boolean isIndexZip() throws RemoteException
281    {
282        return indezZip;
283    }
284
285
286    @Override
287    public void setIndexZip(boolean value) throws RemoteException {
288        indezZip = value;
289    }
290
291    /**
292     * @return the monitorWatcher
293     */
294    public boolean isMonitorWatcher() {
295        return monitorWatcher;
296    }
297
298    /**
299     * @param monitorWatcher the monitorWatcher to set
300     */
301    public void setMonitorWatcher(boolean monitorWatcher) {
302        this.monitorWatcher = monitorWatcher;
303    }
304
305    @Override
306    public boolean isIndexAnonymous() throws RemoteException {
307        return this.indexAnonymous;
308    }
309
310    @Override
311    public void setIndexAnonymous(boolean value) throws RemoteException {
312        this.indexAnonymous = value;
313    }
314
315    @Override
316    public boolean isGZipStorage() throws RemoteException {
317        return this.gzipStorage;
318    }
319
320    @Override
321    public void setGZipStorage(boolean value) throws RemoteException {
322        this.gzipStorage = value;
323    }
324
325}