1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.common.mapping;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
19
|
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
|
20
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
21
|
import eu.etaxonomy.cdm.model.media.Media;
|
22
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
23
|
|
24
|
/**
|
25
|
* This class adds a Media to a DescriptionElementBase
|
26
|
* @author a.mueller
|
27
|
* @since 12.03.2010
|
28
|
*/
|
29
|
public class DbImportMediaMapper extends DbImportMultiAttributeMapperBase<DescriptionElementBase, DbImportStateBase<?,?>> {
|
30
|
private static final Logger logger = Logger.getLogger(DbImportMediaMapper.class);
|
31
|
|
32
|
//********************************** FACTORY ***************************************
|
33
|
|
34
|
public static DbImportMediaMapper NewInstance(String dbUriAttribute){
|
35
|
return new DbImportMediaMapper(dbUriAttribute, null);
|
36
|
}
|
37
|
|
38
|
|
39
|
public static DbImportMediaMapper NewInstance(String dbFirstUriAttribute, String dbSecondUriAttribute){
|
40
|
return new DbImportMediaMapper(dbFirstUriAttribute, dbSecondUriAttribute);
|
41
|
}
|
42
|
|
43
|
//********************************** VARIABLES ***************************************
|
44
|
|
45
|
private String dbFirstUriAttribute;
|
46
|
private String dbSecondUriAttribute;
|
47
|
|
48
|
//********************************** METHODS ***************************************
|
49
|
|
50
|
public DbImportMediaMapper(String dbFirstUriAttribute, String dbSecondUriAttribute) {
|
51
|
this.dbFirstUriAttribute = dbFirstUriAttribute;
|
52
|
this.dbSecondUriAttribute = dbSecondUriAttribute;
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
public DescriptionElementBase invoke(ResultSet rs, DescriptionElementBase element) throws SQLException {
|
57
|
String uri1String = getStringDbValue(rs, dbFirstUriAttribute);
|
58
|
String uri2String = getStringDbValue(rs, dbSecondUriAttribute);
|
59
|
Integer size = null;
|
60
|
String mimeType = null;
|
61
|
String suffix = null;
|
62
|
|
63
|
URI uri1 = null;
|
64
|
try {
|
65
|
uri1 = new URI(uri1String);
|
66
|
} catch (URISyntaxException e) {
|
67
|
String warning = "URISyntaxException when trying to convert first uri string: " + uri1String;
|
68
|
logger.error(warning);
|
69
|
}
|
70
|
URI uri2 = null;
|
71
|
try {
|
72
|
uri2 = new URI(uri2String);
|
73
|
} catch (URISyntaxException e) {
|
74
|
String warning = "URISyntaxException when trying to convert first uri string: " + uri1String;
|
75
|
logger.error(warning);
|
76
|
}
|
77
|
Media media = Media.NewInstance(uri1, size, mimeType, suffix);
|
78
|
if (media != null){
|
79
|
MediaRepresentation secondRepresentation = MediaRepresentation.NewInstance(mimeType, suffix, uri2, size, null);
|
80
|
media.addRepresentation(secondRepresentation);
|
81
|
}else{
|
82
|
media = Media.NewInstance(uri2, size, mimeType, suffix);
|
83
|
}
|
84
|
element.addMedia(media);
|
85
|
return element;
|
86
|
}
|
87
|
}
|