Project

General

Profile

Download (3.01 KB) Statistics
| Branch: | Tag: | Revision:
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
 * @created 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

    
44
//********************************** VARIABLES ***************************************
45
	private String dbFirstUriAttribute;
46
	private String dbSecondUriAttribute;
47

    
48

    
49
//********************************** METHODS ***************************************
50

    
51

    
52
	public DbImportMediaMapper(String dbFirstUriAttribute, String dbSecondUriAttribute) {
53
		super();
54
		this.dbFirstUriAttribute = dbFirstUriAttribute;
55
		this.dbSecondUriAttribute = dbSecondUriAttribute;
56
	}
57

    
58
	@Override
59
	public DescriptionElementBase invoke(ResultSet rs, DescriptionElementBase element) throws SQLException {
60
		String uri1String = getStringDbValue(rs, dbFirstUriAttribute);
61
		String uri2String = getStringDbValue(rs, dbSecondUriAttribute);
62
		Integer size = null;
63
		String mimeType = null;
64
		String suffix = null;
65

    
66
		URI uri1 = null;
67
		try {
68
			uri1 = new URI(uri1String);
69
		} catch (URISyntaxException e) {
70
			String warning = "URISyntaxException when trying to convert first uri string: " + uri1String;
71
			logger.error(warning);
72
		}
73
		URI uri2 = null;
74
		try {
75
			uri2 = new URI(uri2String);
76
		} catch (URISyntaxException e) {
77
			String warning = "URISyntaxException when trying to convert first uri string: " + uri1String;
78
			logger.error(warning);
79
		}
80
		Media media = Media.NewInstance(uri1, size, mimeType, suffix);
81
		if (media != null){
82
			MediaRepresentation secondRepresentation = MediaRepresentation.NewInstance(mimeType, suffix, uri2, size, null);
83
			media.addRepresentation(secondRepresentation);
84
		}else{
85
			media = Media.NewInstance(uri2, size, mimeType, suffix);
86
		}
87
		element.addMedia(media);
88
		return element;
89
	}
90

    
91

    
92
}
(24-24/51)