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.plugins;
020
021import java.io.BufferedInputStream;
022import java.io.File;
023import java.io.FileInputStream;
024import java.io.IOException;
025import java.io.InputStream;
026import java.net.URI;
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.Iterator;
030import java.util.List;
031
032import metal.utils.fileiterator.FileIterator;
033
034import org.dcm4che2.data.DicomObject;
035import org.dcm4che2.io.DicomInputStream;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039import pt.ua.dicoogle.sdk.PluginBase;
040import pt.ua.dicoogle.sdk.StorageInputStream;
041import pt.ua.dicoogle.sdk.StorageInterface;
042
043public class DefaultFileStoragePlugin extends PluginBase implements StorageInterface{
044
045        private static final Logger logger = LoggerFactory.getLogger(DefaultFileStoragePlugin.class);
046        
047        private String defaultScheme = "file";
048        
049        public DefaultFileStoragePlugin() {
050                super();
051                super.storagePlugins.add(this);
052        }
053
054        @Override
055        public boolean enable() {
056                return true;
057        }
058
059        @Override
060        public boolean disable() {
061                return false;
062        }
063
064        @Override
065        public boolean isEnabled() {
066                return true;
067        }
068
069        @Override
070        public String getScheme() {
071                return defaultScheme;
072        }
073
074        @Override
075        public boolean handles(URI location) {
076                if (location.getScheme() == null)
077                        return true;
078                return location.getScheme().equals(defaultScheme);
079        }
080        
081        private Iterator<StorageInputStream> createIterator(URI location){
082                if(!handles(location)){
083                        logger.error("Cannot Handle: "+location.toString());
084                        return Collections.emptyIterator();
085                }
086
087                logger.debug("Stared creating Iterator in: "+location.getSchemeSpecificPart()); 
088                File parent = new File(location.getSchemeSpecificPart());
089                Iterator<File> fileIt;
090                
091                if (parent.isDirectory()) {
092                        logger.debug("Location is a directory: "+location.getSchemeSpecificPart());
093                        fileIt = new FileIterator(parent);
094                } else {
095                        List<File> files = new ArrayList<>(1);
096                        files.add(parent);
097                        fileIt = files.iterator();
098                }
099
100                logger.debug("Finished assembling Iterator: "+ location.getSchemeSpecificPart());
101                return new MyIterator(fileIt);
102        }
103
104        @Override
105        public Iterable<StorageInputStream> at(URI location, Object ... args) {
106                return new MyIterable(location);                
107        }
108
109        @Override
110        public URI store(DicomObject dicomObject, Object ... args) {
111                return null;
112        }
113
114        @Override
115        public URI store(DicomInputStream inputStream, Object ... args) throws IOException {
116                return null;
117        }
118
119        @Override
120        public void remove(URI location) {
121                if (!location.getScheme().equals(defaultScheme)) {
122                        return;
123                }
124
125                String path = location.getSchemeSpecificPart();
126                File f = new File(path);
127                if (f.exists()) {
128                        f.delete();
129                }               
130        }
131
132        @Override
133        public String getName() {
134                return "default-filesystem-plugin";
135        }
136
137        private class MyIterable implements Iterable<StorageInputStream>{
138
139                private URI baseLocation;
140
141                public MyIterable(URI baseLocation) {
142                        super();
143                        this.baseLocation = baseLocation;
144                }
145
146                @Override
147                public Iterator<StorageInputStream> iterator() {
148                        // TODO Auto-generated method stub
149                        return createIterator(baseLocation);
150                }
151                
152        }
153        
154        private static class MyIterator implements Iterator<StorageInputStream>{
155        
156                private Iterator<File> it;
157                
158                public MyIterator(Iterator<File> it) {
159                        super();
160                        this.it = it;
161                }
162
163                @Override
164                public boolean hasNext() {
165                        return it.hasNext();
166                }
167
168                @Override
169                public StorageInputStream next() {
170                        if(!it.hasNext())
171                                return null;
172                        File f = it.next();
173                        logger.debug("Added File: "+f.toURI());
174                        MyDICOMInputString stream = new MyDICOMInputString(f);                  
175                        return stream;
176                }
177
178                @Override
179                public void remove() {
180                        // TODO Auto-generated method stub
181                        
182                }
183        }
184        
185        private static class MyDICOMInputString implements StorageInputStream{
186                
187                private File file;
188
189                public MyDICOMInputString(File file) {
190                        super();
191                        this.file = file;
192                }
193
194                @Override
195                public URI getURI() {
196                        return file.toURI();
197                }
198
199                @Override
200                public InputStream getInputStream() throws IOException {
201                        // TODO Auto-generated method stub
202                        return new BufferedInputStream(new FileInputStream(file));
203                }
204
205                @Override
206                public long getSize() throws IOException {
207                        // TODO Auto-generated method stub
208                        return file.length();
209                }
210                
211        }
212                
213}