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