Source code: medi/util/MP3File2Db.java
1 /*
2 * MP3File2Db.java
3 *
4 * Created on 19 giugno 2002, 9.25
5 Medi - A media archiver. Archives media files for easy management.
6 Copyright (C) 2002 Antonio Petrelli
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package medi.util;
26
27 import java.io.*;
28 import java.text.SimpleDateFormat;
29 import java.util.*;
30 import javatools.io.FileUtils;
31 import javatools.db.*;
32 import javatools.util.StringUtils;
33 import medi.base.Provider;
34 import helliker.id3.*;
35
36 /**
37 * Maps an MP3 into the database, storing information about the title, sampling,
38 * author, album.
39 * Thanks go to the creator of jd3lib.
40 * @author Antonio Petrelli
41 * @version 0.0.1
42 */
43 public class MP3File2Db extends medi.util.AbstractFile2Db {
44
45 /** Creates new MP3File2Db */
46 public MP3File2Db() {
47 extensions = new String[1];
48 extensions[0] = "MP3";
49 author2ID = new HashMap();
50 genre2ID = new HashMap();
51 dataSet2ID = new HashMap();
52 }
53
54 /** Sets the provider to use.
55 * @param pPrv The provider to use.
56 */
57 public void setProvider(Provider pPrv) {
58 DbIterator rowIt;
59 DbRow tempRow;
60 String completeName, tempString;
61
62 super.setProvider(pPrv);
63 try {
64 rowIt = prv.getAuthors(null).iterator();
65 while (rowIt.hasNextRow()) {
66 completeName = null;
67 tempRow = rowIt.nextRow();
68 tempString = (String) tempRow.getValue(1);
69 if (tempString != null)
70 completeName = tempString;
71 tempString = (String) tempRow.getValue(2);
72 if (tempString != null)
73 completeName += (" " + tempString);
74 tempString = (String) tempRow.getValue(3);
75 if (tempString != null)
76 completeName += (" " + tempString);
77 if (completeName != null)
78 completeName = StringUtils.proper(completeName.trim());
79 author2ID.put(completeName, tempRow.getValue(0));
80 }
81 }
82 catch (DbException e) {
83 System.out.println(e.getMessage());
84 }
85 }
86
87 /** Processes a file.
88 * @return The newly created data ID.
89 * @param procFile The file to process.
90 * @param fileTypeID Its file type ID.
91 * @throws DbException If something goes wrong.
92 */
93 public Long process(File procFile, Integer fileTypeID) throws DbException {
94 DbIterator rowIt;
95 MP3File curFile;
96 String tempFilePath, extension, tempName;
97 SimpleDateFormat formatter;
98 String title, author, genre, dataSet;
99 long duration;
100 Long dataID, dataSetID;
101 Integer authorID, genreID;
102
103 dataID = null;
104 try {
105 curFile = new MP3File(procFile);
106 tempFilePath = procFile.getPath();
107 formatter = prv.getDateFormat();
108
109 // System.out.println("File: "+tempFilePath);
110
111 tempName = FileUtils.getFileNameWithNoExtension(tempFilePath);
112 title = curFile.getTitle();
113 if (title != null) {
114 title = title.trim();
115 if (title.compareTo("") == 0)
116 title = null;
117 }
118 if (title == null)
119 title = guessTitle(tempName);
120 if (title != null)
121 title = StringUtils.proper(title);
122 author = curFile.getArtist();
123 if (author != null) {
124 author = author.trim();
125 if (author.trim().compareTo("") == 0)
126 author = null;
127 }
128 if (author == null)
129 author = guessAuthor(tempName);
130 if (author != null)
131 author = StringUtils.proper(author);
132 genre = curFile.getGenre();
133 if (genre != null) {
134 genre = genre.trim();
135 if (genre.compareTo("") == 0)
136 genre = null;
137 else
138 genre = StringUtils.proper(genre);
139 }
140 dataSet = curFile.getAlbum();
141 if (dataSet != null) {
142 dataSet = dataSet.trim();
143 if (dataSet.trim().compareTo("") == 0)
144 dataSet = null;
145 else
146 dataSet = StringUtils.proper(dataSet);
147 }
148 duration = curFile.getPlayingTime();
149 /*extension = FileUtils.getExtension(tempFilePath).toUpperCase();
150 rowIt = prv.getFileTypesByExtension(extension).iterator();
151 if (rowIt.hasNextRow())
152 fileTypeID = (Integer) rowIt.nextRow().getValue(0);
153 else
154 fileTypeID =
155 new Integer(prv.createNewFileType(extension+" file", extension, new Integer(0), null));
156 */
157 dataID = new Long(prv.createNewData(fileTypeID, title,
158 FileUtils.getFileName(tempFilePath), formatter.format(new Date(procFile.lastModified())),
159 new Long(procFile.length()), null, new Integer(new Long(duration).intValue()),
160 new Integer(16), new Integer(curFile.getSampleRate()), new Integer(1),
161 new Integer(curFile.getBitRate()), null, null));
162 if (author != null) {
163 authorID = (Integer) author2ID.get(author);
164 if (authorID == null)
165 authorID = new Integer(prv.createNewAuthor("", null, author,
166 null, null, null, null));
167 prv.linkDataToAuthor(dataID, authorID);
168 author2ID.put(author, authorID);
169 }
170 if (genre != null) {
171 genreID = findGenre(genre);
172 prv.linkDataToGenre(dataID, genreID);
173 }
174 if (dataSet != null) {
175 dataSetID = findDataSet(dataSet);
176 prv.linkDataToDataSet(dataID, dataSetID);
177 }
178 }
179 catch (FileNotFoundException e) {
180 System.out.println(e.getMessage());
181 }
182 catch (IOException e) {
183 System.out.println(e.getMessage());
184 }
185 catch (ID3v2FormatException e) {
186 System.out.println(e.getMessage());
187 }
188 catch (NoMPEGFramesException e) {
189 System.out.println(e.getMessage());
190 }
191 catch (CorruptHeaderException e) {
192 System.out.println(e.getMessage());
193 }
194 return dataID;
195 }
196
197 private String guessTitle(String filePath) {
198 String tempString;
199 int pos;
200
201 pos = filePath.indexOf('-');
202 if (pos < 0)
203 tempString = filePath;
204 else {
205 tempString = filePath.substring(pos+1).trim();
206 if (tempString.compareTo("") == 0)
207 tempString = filePath;
208 }
209 return tempString;
210 }
211
212 private String guessAuthor(String filePath) {
213 String tempString;
214 int pos;
215
216 pos = filePath.indexOf('-');
217 if (pos < 0)
218 tempString = null;
219 else {
220 tempString = filePath.substring(0, pos-1).trim();
221 if (tempString.compareTo("") == 0)
222 tempString = null;
223 }
224 return tempString;
225 }
226
227 private Integer findGenre(String name) throws DbException {
228 DbIterator rowIt;
229 Integer tempID;
230
231 tempID = (Integer) genre2ID.get(name);
232 if (tempID == null) {
233 rowIt = prv.getGenresByName(name).iterator();
234 if (rowIt.hasNextRow())
235 tempID = (Integer) rowIt.nextRow().getValue(0);
236 else
237 tempID = new Integer(prv.createNewGenre(null, name, null));
238 genre2ID.put(name, tempID);
239 }
240 return tempID;
241 }
242
243 private Long findDataSet(String name) throws DbException {
244 DbIterator rowIt;
245 Long tempID;
246
247 tempID = (Long) dataSet2ID.get(name);
248 if (tempID == null) {
249 rowIt = prv.getDataSetsByName(name).iterator();
250 if (rowIt.hasNextRow())
251 tempID = (Long) rowIt.nextRow().getValue(0);
252 else
253 tempID = new Long(prv.createNewDataSet(name, null));
254 dataSet2ID.put(name, tempID);
255 }
256 return tempID;
257 }
258
259 private HashMap author2ID, genre2ID, dataSet2ID;
260 }