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
021import java.io.File;
022import java.net.URI;
023import java.net.URISyntaxException;
024import java.rmi.RemoteException;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.concurrent.Semaphore;
028import org.slf4j.LoggerFactory;
029
030import pt.ua.dicoogle.core.TagsXML;
031import pt.ua.dicoogle.core.dicom.PrivateDictionary;
032import pt.ua.dicoogle.plugins.PluginController;
033import pt.ua.dicoogle.rGUI.interfaces.controllers.IIndexOptions;
034import pt.ua.dicoogle.rGUI.server.DicoogleScan;
035import pt.ua.dicoogle.sdk.utils.TagValue;
036import pt.ua.dicoogle.sdk.utils.TagsStruct;
037
038/**
039 * Controller of Index Options Settings
040 *
041 * @author Samuel Campos <samuelcampos@ua.pt>
042 */
043@Deprecated
044public class IndexOptions implements IIndexOptions {
045
046    //private HashMap<Integer, TagValue> dimFields;
047    //private ArrayList<String> modalities;
048    //private HashMap<Integer, TagValue> manualFields;
049    private boolean isIndexAllModalities;
050
051    private static Semaphore sem = new Semaphore(1, true);
052    private static IndexOptions instance = null;
053
054    private TagsStruct tags = TagsStruct.getInstance();
055    private boolean saved = true;
056
057    public static synchronized IndexOptions getInstance()
058    {
059        try {
060            sem.acquire();
061            if (instance == null) {
062                instance = new IndexOptions();
063            }
064            sem.release();
065        } catch (InterruptedException ex) {
066            LoggerFactory.getLogger(QRServers.class).error(ex.getMessage(), ex);
067        }
068        return instance;
069    }
070    
071    private IndexOptions(){
072        loadSettings(); 
073    }
074
075    /**
076     * Load settings from TagsStruct
077     */
078    public void loadSettings(){
079        isIndexAllModalities = tags.isIndexAllModalitiesEnabled();
080    }
081
082    /**
083     * Save the Settings related to IndexOptions (TagsStruct)
084     *
085     * Print TagsXML
086     */
087    public void saveSettings(){
088        saved = true;
089        tags.enableIndexAllModalities(isIndexAllModalities);
090
091        // Flush to XML file
092        TagsXML ts = new TagsXML();
093        ts.printXML();
094    }
095
096
097    /**
098     *
099     * @return  true - if there are unsaved settings ( != TagsStruct)
100     *          false - not
101     */
102    public boolean unsavedSettings(){
103        return saved;
104    }
105
106    @Override
107    public HashMap<String, ArrayList<TagValue>> getDIMFields() throws RemoteException {
108        //DebugManager.getInstance().debug("Getting DIM Fields");        
109        /** Get groups **/
110        HashMap<String, ArrayList<TagValue>> groupTable = new HashMap<String, ArrayList<TagValue>>();      
111        for(TagValue tag : tags.getDIMFields()){
112            if (groupTable.containsKey(tag.getGroup())) {
113                groupTable.get(tag.getGroup()).add(tag);
114
115            } else {
116                ArrayList<TagValue> temp = new ArrayList<TagValue>();
117                temp.add(tag);
118                groupTable.put(tag.getGroup(), temp);
119            }
120        }
121        
122        return groupTable;
123    }
124
125    @Override
126    public ArrayList<String> getModalities() throws RemoteException {
127        ArrayList<String> arr = new ArrayList<>(tags.getModalities());          
128        return arr;
129    }
130
131    @Override
132    public boolean addModality(String modality) throws RemoteException {
133        saved = false;
134        boolean r = !tags.containsModality(modality);
135        tags.addModality(modality);
136        return r;
137    }
138
139    @Override
140    public boolean removeModality(String modality) throws RemoteException {
141        saved = false;
142    
143        return tags.removeModality(modality);
144    }
145
146    @Override
147    public HashMap<String, ArrayList<TagValue>> getManualFields() throws RemoteException {
148        /** Get groups **/
149        HashMap<String, ArrayList<TagValue>> groupTable = new HashMap<String, ArrayList<TagValue>>();
150        for (TagValue tag : tags.getOtherFields()) {
151            //DebugManager.getInstance().debug(">> Grouping the Others Tags in MainWindow Notebook");
152
153            if (groupTable.containsKey(tag.getGroup())) {
154                groupTable.get(String.valueOf(tag.getGroup())).add(tag);
155
156            } else {
157                ArrayList<TagValue> temp = new ArrayList<TagValue>();
158                temp.add(tag);
159                groupTable.put(tag.getGroup(), temp);
160            }
161        }
162
163        return groupTable;
164    }
165
166    @Override
167    public boolean addManualField(int group, int subGroup, String name) throws RemoteException {
168        saved = false;
169        String tag = String.valueOf(group) + String.valueOf(subGroup);
170        TagValue t = new TagValue(Integer.parseInt(tag, 16), name);       
171        
172        return tags.addPrivateField(t);
173    }
174
175    @Override
176    public boolean removeManualField(int group, int subGroup) throws RemoteException {
177        saved = false;
178        String tag = String.valueOf(group) + String.valueOf(subGroup);
179
180        return tags.removePrivateField(Integer.parseInt(tag, 16));
181    }
182
183    @Override
184    public boolean isIndexAllModalities() throws RemoteException {
185        return isIndexAllModalities;
186    }
187
188    @Override
189    public void setIndexAllModalities(boolean value) throws RemoteException {
190        saved = false;
191        isIndexAllModalities = value;
192    }
193
194
195
196
197    /**
198     * Index new file or folder
199     *
200     * @param Path - file or folter path
201     * @throws RemoteException
202     */
203    @Override
204    public void index(String Path, boolean resume) throws RemoteException {
205        Logs.getInstance().addServerLog("Start indexing: " + Path);
206        
207        DicoogleScan scandir = new DicoogleScan(Path);
208        scandir.scan(resume);
209    }
210
211    /**
212     * Indexes even if the file is already indexed
213     *
214     * @param path
215     * @throws RemoteException
216     */
217    @Override
218    public void reIndex(ArrayList<String> list) throws RemoteException {
219        Logs.getInstance().addServerLog("Start reIndexing " + list.size() + " file(s)");
220        
221        class REIndex extends Thread{
222            private ArrayList<String> list;
223
224            public REIndex(ArrayList<String> list){
225                this.list = list;
226            }
227
228            @Override
229            public void run(){
230                for(String Path: list)
231                    try {
232                        PluginController.getInstance().index(new URI(Path));
233                    } catch (URISyntaxException ex) {
234                        LoggerFactory.getLogger(DirectorySettings.class).error(ex.getMessage(), ex);
235                    }
236            }
237        }
238
239        //reIndex all files
240        (new REIndex(list)).start();
241    }
242
243    @Override
244    public void removeFilesFromIndexer(ArrayList<String> files, boolean deleteFiles) throws RemoteException {
245        Logs.getInstance().addServerLog("Start removing " + files.size() + " file(s)");
246
247        class DeleteFiles extends Thread{
248            private ArrayList<String> list;
249            private boolean delete;
250
251            public DeleteFiles(ArrayList<String> list, boolean delete){
252                this.list = list;
253                this.delete = delete;
254            }
255
256            @Override
257            public void run(){
258                for(String Path: list)
259                {
260                    try {
261                        PluginController.getInstance().index(new URI(Path));
262                    } catch (URISyntaxException ex) {
263                        LoggerFactory.getLogger(DirectorySettings.class).error(ex.getMessage(), ex);
264                    }
265                
266                    if (delete)
267                    {
268                        
269                        File f = new File(Path);
270                        f.delete();
271                    }
272                }
273            }
274        }
275
276        (new DeleteFiles(files, deleteFiles)).start();
277
278    }
279
280    @Override
281    public boolean removeDictionary(String dic) throws RemoteException {
282        
283        TagsStruct.getInstance().removeDicionary(dic);
284        
285        return true;
286        
287    }
288
289    @Override
290    public boolean addDictionary(String dic) throws RemoteException {
291        
292        TagsStruct.getInstance().addDicionary(dic);
293        PrivateDictionary pd = new PrivateDictionary();
294        pd.parse(dic);
295        return true;
296        
297    }
298
299}