View Javadoc

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.berlinModel.out.mapper;
12  
13  import java.sql.PreparedStatement;
14  import java.sql.SQLException;
15  
16  import org.apache.log4j.Logger;
17  
18  import eu.etaxonomy.cdm.common.CdmUtils;
19  import eu.etaxonomy.cdm.io.common.DbExportStateBase;
20  import eu.etaxonomy.cdm.io.common.mapping.MultipleAttributeMapperBase;
21  import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
22  import eu.etaxonomy.cdm.model.common.Annotation;
23  import eu.etaxonomy.cdm.model.common.AnnotationType;
24  import eu.etaxonomy.cdm.model.common.CdmBase;
25  
26  /**
27   * @author a.mueller
28   * @created 12.05.2009
29   * @version 1.0
30   */
31  public class CreatedAndNotesMapper extends MultipleAttributeMapperBase<DbSingleAttributeExportMapperBase<DbExportStateBase<?>>> implements IDbExportMapper<DbExportStateBase<?>>{
32  	@SuppressWarnings("unused")
33  	private static final Logger logger = Logger.getLogger(CreatedAndNotesMapper.class);
34  	
35  	public static CreatedAndNotesMapper NewInstance(){
36  		return new CreatedAndNotesMapper(true);
37  	}
38  	
39  	public static CreatedAndNotesMapper NewInstance(boolean withUpdate){
40  		return new CreatedAndNotesMapper(withUpdate);
41  	}
42  
43  	
44  	/**
45  	 * @param dbAttributString
46  	 * @param cdmAttributeString
47  	 */
48  	private CreatedAndNotesMapper(boolean withUpdate) {
49  		singleMappers.add(DbUserMapper.NewInstance("createdBy", "Created_Who"));
50  		singleMappers.add(DbDateMapper.NewInstance("created", "Created_When"));
51  		singleMappers.add(MethodMapper.NewInstance("Notes", this.getClass(), "getNotes", AnnotatableEntity.class));
52  		if (withUpdate){
53  			singleMappers.add(DbUserMapper.NewInstance("updatedBy", "Updated_Who"));
54  			singleMappers.add(DbDateMapper.NewInstance("updated", "Updated_When"));
55  		}
56  	}
57  
58  
59  	/* (non-Javadoc)
60  	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.IDbExportMapper#initialize(java.sql.PreparedStatement, eu.etaxonomy.cdm.io.berlinModel.out.mapper.IndexCounter, eu.etaxonomy.cdm.io.berlinModel.out.DbExportState)
61  	 */
62  	public void initialize(PreparedStatement stmt, IndexCounter index, DbExportStateBase<?> state, String tableName) {
63  		for (DbSingleAttributeExportMapperBase<DbExportStateBase<?>> mapper : singleMappers){
64  			mapper.initialize(stmt, index, state, tableName);
65  		}	
66  	}
67  
68  
69  	/* (non-Javadoc)
70  	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.IDbExportMapper#invoke(eu.etaxonomy.cdm.model.common.CdmBase)
71  	 */
72  	public boolean invoke(CdmBase cdmBase) throws SQLException {
73  		boolean result = true;
74  		for (DbSingleAttributeExportMapperBase<DbExportStateBase<?>> mapper : singleMappers){
75  			result &= mapper.invoke(cdmBase);
76  		}
77  		return result;
78  	}
79  	
80  	//used by MethodMapper
81  	@SuppressWarnings("unused")
82  	private static String getNotes(AnnotatableEntity obj){
83  		String result = "";
84  		String separator = ";";
85  		for (Annotation annotation :obj.getAnnotations()){
86  			if (! AnnotationType.TECHNICAL().equals(annotation.getAnnotationType())){
87  				if (! ( annotation.getText() != null && annotation.getText().startsWith("ORDER:"))){  //TODO casus Salvador, should be stored in Extension ones extensions are also for annotatable entities
88  					result = CdmUtils.concat(separator, result, annotation.getText());
89  				}
90  			}
91  		}
92  		return (result.trim().equals("")? null : result);
93  	}
94  	
95  }