handle files in alphabetical order
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / metadata / DirectoryMetadata.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.htmlindexer.metadata;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.io.Serializable;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Iterator;
21
22 import eu.svjatoslav.commons.file.FilePathParser;
23 import eu.svjatoslav.meviz.htmlindexer.AbstractIndexer;
24 import eu.svjatoslav.meviz.htmlindexer.FileSortComparator;
25 import eu.svjatoslav.meviz.htmlindexer.ImageFormatError;
26 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
27 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.DirectoryFile;
28 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.GeneralFile;
29 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
30
31 /**
32  * Corresponds to single filesystem directory.
33  */
34 public class DirectoryMetadata implements Serializable {
35
36         private static final long serialVersionUID = 3526489102725366334L;
37
38         private final HashMap<String, AbstractFile> fileNameToFileMap = new HashMap<String, AbstractFile>();
39
40         public transient boolean changed;
41
42         /**
43          * Performs cleanup of directory model. In particular removes metadata for
44          * images that no longer exist in the filesystem.
45          */
46         public void cleanup() {
47                 final Iterator<AbstractFile> fileIterator = fileNameToFileMap.values()
48                                 .iterator();
49
50                 while (fileIterator.hasNext()) {
51                         final AbstractFile file = fileIterator.next();
52
53                         if (!file.isMetaInfoVerified())
54                                 fileIterator.remove();
55                 }
56         }
57
58         public void ensureFileMetainfoIsUpToDate(final File parentDirectory,
59                         final File file) throws IOException, ImageFormatError {
60
61                 final String fileName = file.getName();
62
63                 if (fileNameToFileMap.containsKey(fileName)) {
64                         final boolean metainfoUpdated = fileNameToFileMap.get(fileName)
65                                         .ensureFileMetainfoIsUpToDate(parentDirectory);
66
67                         if (metainfoUpdated)
68                                 changed = true;
69                 } else
70                         registerFile(parentDirectory, file);
71
72         }
73
74         public AbstractFile getFile(final String fileName) throws IOException {
75                 return fileNameToFileMap.get(fileName);
76         }
77
78         public Collection<AbstractFile> getFiles() {
79
80                 final ArrayList<AbstractFile> files = new ArrayList<AbstractFile>(
81                                 fileNameToFileMap.values());
82
83                 Collections.sort(files, new FileSortComparator());
84
85                 return files;
86         }
87
88         public Dimension getImageDimensions(final String fileName)
89                         throws IOException, ImageFormatError {
90
91                 return ((Picture) getFile(fileName)).getDimensions();
92         }
93
94         public AbstractFile registerFile(final File parentDirectory, final File file)
95                         throws IOException, ImageFormatError {
96
97                 final String fileName = file.getName();
98
99                 final AbstractFile abstractFile;
100
101                 if (file.isDirectory())
102                         abstractFile = new DirectoryFile(parentDirectory, fileName);
103                 else {
104                         final String fileExtension = FilePathParser.getFileExtension(
105                                         fileName).toLowerCase();
106
107                         if (AbstractIndexer.isImage(fileExtension))
108                                 abstractFile = new Picture(parentDirectory, fileName);
109                         else
110                                 abstractFile = new GeneralFile(parentDirectory, fileName);
111                 }
112
113                 fileNameToFileMap.put(fileName, abstractFile);
114
115                 changed = true;
116
117                 return abstractFile;
118         }
119
120         public void removeUnusedMetadataEntries() {
121
122                 for (final String key : new HashSet<String>(fileNameToFileMap.keySet()))
123                         if (!fileNameToFileMap.get(key).isMetaInfoVerified()) {
124                                 fileNameToFileMap.remove(key);
125                                 changed = true;
126                         }
127
128         }
129 }