cb2c2b14012da7e160c2e055752545d423b4d628
[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.MevizSpecialFile;
30 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
31
32 /**
33  * Corresponds to single filesystem directory.
34  */
35 public class DirectoryMetadata implements Serializable {
36
37         private static final long serialVersionUID = 3526489102725366334L;
38
39         private final HashMap<String, AbstractFile> fileNameToFileMap = new HashMap<String, AbstractFile>();
40
41         public transient boolean changed;
42
43         /**
44          * Performs cleanup of directory model. In particular removes metadata for
45          * images that no longer exist in the filesystem.
46          */
47         public void cleanup() {
48                 final Iterator<AbstractFile> fileIterator = fileNameToFileMap.values()
49                                 .iterator();
50
51                 while (fileIterator.hasNext()) {
52                         final AbstractFile file = fileIterator.next();
53
54                         if (!file.isMetaInfoVerified())
55                                 fileIterator.remove();
56                 }
57         }
58
59         public void ensureFileMetainfoIsUpToDate(final File parentDirectory,
60                         final File file) throws Exception {
61
62                 final String fileName = file.getName();
63
64                 if (fileNameToFileMap.containsKey(fileName)) {
65                         final boolean metainfoUpdated = fileNameToFileMap.get(fileName)
66                                         .ensureFileMetainfoIsUpToDate(parentDirectory);
67
68                         if (metainfoUpdated)
69                                 changed = true;
70                 } else
71                         registerFile(parentDirectory, file);
72
73         }
74
75         private AbstractFile getAbstractFile(final File parentDirectory,
76                         final File file, final String fileName) throws Exception {
77
78                 if (file.isDirectory())
79                         return new DirectoryFile(parentDirectory, fileName);
80
81                 final String fileExtension = FilePathParser.getFileExtension(fileName)
82                                 .toLowerCase();
83
84                 if (AbstractIndexer.isImage(fileExtension))
85                         try {
86                                 return new Picture(parentDirectory, fileName);
87                         } catch (final Exception exception) {
88                                 // in case image decoding failed, handle image as general file
89                                 return new GeneralFile(parentDirectory, fileName);
90                         }
91
92                 if (fileName.endsWith(".meviz.org"))
93                         return new MevizSpecialFile(parentDirectory, fileName);
94
95                 if (fileName.endsWith(".meviz.html"))
96                         return new MevizSpecialFile(parentDirectory, fileName);
97
98                 return new GeneralFile(parentDirectory, fileName);
99         }
100
101         public AbstractFile getFile(final String fileName) throws IOException {
102                 return fileNameToFileMap.get(fileName);
103         }
104
105         public Collection<AbstractFile> getFiles() {
106
107                 final ArrayList<AbstractFile> files = new ArrayList<AbstractFile>(
108                                 fileNameToFileMap.values());
109
110                 Collections.sort(files, new FileSortComparator());
111
112                 return files;
113         }
114
115         public Dimension getImageDimensions(final String fileName)
116                         throws IOException, ImageFormatError {
117
118                 return ((Picture) getFile(fileName)).getDimensions();
119         }
120
121         public AbstractFile registerFile(final File parentDirectory, final File file)
122                         throws Exception {
123
124                 final String fileName = file.getName();
125
126                 final AbstractFile abstractFile = getAbstractFile(parentDirectory,
127                                 file, fileName);
128
129                 fileNameToFileMap.put(fileName, abstractFile);
130
131                 changed = true;
132
133                 return abstractFile;
134         }
135
136         public void removeUnusedMetadataEntries() {
137
138                 for (final String key : new HashSet<String>(fileNameToFileMap.keySet()))
139                         if (!fileNameToFileMap.get(key).isMetaInfoVerified()) {
140                                 fileNameToFileMap.remove(key);
141                                 changed = true;
142                         }
143
144         }
145 }