Project

General

Profile

Revision 3ef01914

ID3ef01914f57445c17dfa1977b7632c88908ab69a
Parent 41228061
Child 8faab8c3

Added by Andreas Müller about 3 years ago

ref #6754 remove Component from single DwcA exports

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaDescriptionExport.java
9 9

  
10 10
package eu.etaxonomy.cdm.io.dwca.out;
11 11

  
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
12 14
import java.io.PrintWriter;
15
import java.io.UnsupportedEncodingException;
13 16
import java.util.ArrayList;
14 17
import java.util.List;
15 18
import java.util.Set;
16 19

  
17 20
import org.apache.log4j.Logger;
18
import org.springframework.stereotype.Component;
19 21
import org.springframework.transaction.TransactionStatus;
20 22

  
21 23
import eu.etaxonomy.cdm.model.common.CdmBase;
......
32 34
 * @author a.mueller
33 35
 * @created 20.04.2011
34 36
 */
35
@Component
36 37
public class DwcaDescriptionExport extends DwcaExportBase {
37 38

  
38 39
    private static final long serialVersionUID = 4756084824053120718L;
......
42 43
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Description";
43 44
	protected static final String fileName = "description.txt";
44 45

  
46
    private DwcaMetaDataRecord metaRecord;
47

  
45 48
	/**
46 49
	 * Constructor
47 50
	 */
48
	public DwcaDescriptionExport() {
51
	public DwcaDescriptionExport(DwcaTaxExportState state) {
49 52
		super();
50 53
		this.ioName = this.getClass().getSimpleName();
54
	    metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
55
	    state.addMetaRecord(metaRecord);
56
	    file = DwcaTaxOutputFile.DESCRIPTION;
51 57
	}
52 58

  
53 59
	/**
......
61 67
	 */
62 68
	@Override
63 69
	protected void doInvoke(DwcaTaxExportState state){
64
		DwcaTaxExportConfigurator config = state.getConfig();
65
		TransactionStatus txStatus = startTransaction(true);
66 70

  
67
		DwcaTaxOutputFile file = DwcaTaxOutputFile.DESCRIPTION;
71
	    TransactionStatus txStatus = startTransaction(true);
72

  
68 73
		try {
69 74
			DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
70 75
			state.addMetaRecord(metaRecord);
71 76

  
72 77
            List<TaxonNode> allNodes = allNodes(state);
73 78
			for (TaxonNode node : allNodes){
74
				Taxon taxon = CdmBase.deproxy(node.getTaxon());
75
				Set<TaxonDescription> descriptions = node.getTaxon().getDescriptions();
76
				for (TaxonDescription description : descriptions){
77
					for (DescriptionElementBase el : description.getElements()){
78
						if (el.isInstanceOf(TextData.class) ){
79
							Feature feature = el.getFeature();
80
							if (feature != null &&
81
									! feature.equals(Feature.IMAGE()) &&
82
									! config.getFeatureExclusions().contains(feature.getUuid()) &&
83
									! state.recordExists(file,el)){
84
								DwcaDescriptionRecord record = new DwcaDescriptionRecord(metaRecord, config);
85
								TextData textData = CdmBase.deproxy(el,TextData.class);
86
								handleDescription(record, textData, taxon, config);
87
								PrintWriter writer = createPrintWriter(state, DwcaTaxOutputFile.DESCRIPTION);
88
					            record.write(state, writer);
89
								state.addExistingRecord(file, textData);
90
							}
91
						}
92
					}
93
				}
94

  
95
                flushWriter(state, file);
79
				handleTaxonNode(state, node);
96 80
			}
97 81
		} catch (Exception e) {
98 82
            String message = "Unexpected exception " + e.getMessage();
99 83
            state.getResult().addException(e, message, "DwcaDescriptionExport.doInvoke()");
100 84
		} finally {
101
			closeWriter(file, state);
85
			closeWriter(state);
102 86
		}
103 87
		commitTransaction(txStatus);
104 88
		return;
105 89
	}
106 90

  
91
    /**
92
     * @param state
93
     * @param config
94
     * @param file
95
     * @param metaRecord
96
     * @param node
97
     * @throws IOException
98
     * @throws FileNotFoundException
99
     * @throws UnsupportedEncodingException
100
     */
101
    protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
102
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
103
        DwcaTaxExportConfigurator config = state.getConfig();
104

  
105
        try {
106
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
107
            Set<TaxonDescription> descriptions = node.getTaxon().getDescriptions();
108
            for (TaxonDescription description : descriptions){
109
            	for (DescriptionElementBase el : description.getElements()){
110
            		if (el.isInstanceOf(TextData.class) ){
111
            			Feature feature = el.getFeature();
112
            			if (feature != null &&
113
            					! feature.equals(Feature.IMAGE()) &&
114
            					! config.getFeatureExclusions().contains(feature.getUuid()) &&
115
            					! state.recordExists(file,el)){
116
            				DwcaDescriptionRecord record = new DwcaDescriptionRecord(metaRecord, config);
117
            				TextData textData = CdmBase.deproxy(el,TextData.class);
118
            				handleDescription(record, textData, taxon, config);
119
            				PrintWriter writer = createPrintWriter(state, DwcaTaxOutputFile.DESCRIPTION);
120
            	            record.write(state, writer);
121
            				state.addExistingRecord(file, textData);
122
            			}
123
            		}
124
            	}
125
            }
126
        } catch (Exception e) {
127
            String message = "Unexpected exception: " + e.getMessage();
128
            state.getResult().addException(e, message);
129
        }finally{
130
            flushWriter(state, file);
131
        }
132
    }
133

  
107 134

  
108 135
	private void handleDescription(DwcaDescriptionRecord record, TextData textData, Taxon taxon, DwcaTaxExportConfigurator config) {
109 136
		record.setId(taxon.getId());
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaDistributionExport.java
9 9

  
10 10
package eu.etaxonomy.cdm.io.dwca.out;
11 11

  
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
12 14
import java.io.PrintWriter;
15
import java.io.UnsupportedEncodingException;
13 16
import java.util.List;
14 17
import java.util.Set;
15 18

  
16 19
import org.apache.log4j.Logger;
17
import org.springframework.stereotype.Component;
18 20
import org.springframework.transaction.TransactionStatus;
19 21

  
20 22
import eu.etaxonomy.cdm.model.common.CdmBase;
......
29 31
 * @author a.mueller
30 32
 * @created 20.04.2011
31 33
 */
32
@Component
33 34
public class DwcaDistributionExport extends DwcaExportBase {
34 35

  
35 36
    private static final long serialVersionUID = -3274468345456407430L;
......
39 40
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Distribution";
40 41
	protected static final String fileName = "distribution.txt";
41 42

  
43
    private DwcaMetaDataRecord metaRecord;
44

  
42 45
	/**
43 46
	 * Constructor
44 47
	 */
45
	public DwcaDistributionExport() {
48
	public DwcaDistributionExport(DwcaTaxExportState state) {
46 49
		super();
47 50
		this.ioName = this.getClass().getSimpleName();
51
        metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
52
        state.addMetaRecord(metaRecord);
53
        file = DwcaTaxOutputFile.DISTRIBUTION;
48 54
	}
49 55

  
50 56
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
......
57 63
	 */
58 64
	@Override
59 65
	protected void doInvoke(DwcaTaxExportState state){
60
		DwcaTaxExportConfigurator config = state.getConfig();
61 66
		TransactionStatus txStatus = startTransaction(true);
62 67

  
63
		DwcaTaxOutputFile file = DwcaTaxOutputFile.DISTRIBUTION;
64 68
		try {
65 69
			DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
66 70
			state.addMetaRecord(metaRecord);
67 71

  
68 72
			List<TaxonNode> allNodes = allNodes(state);
69 73
            for (TaxonNode node : allNodes){
70
				Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
71

  
72
				Set<TaxonDescription> descriptions = taxon.getDescriptions();
73
				for (TaxonDescription description : descriptions){
74
					for (DescriptionElementBase el : description.getElements()){
75
						if (el.isInstanceOf(Distribution.class) ){
76
							if (! state.recordExists(file, el)){
77
								DwcaDistributionRecord record = new DwcaDistributionRecord(metaRecord, config);
78
								Distribution distribution = CdmBase.deproxy(el, Distribution.class);
79
								handleDistribution(record, distribution, taxon, config);
80
								PrintWriter writer = createPrintWriter(state, file);
81
					            record.write(state, writer);
82
								state.addExistingRecord(file, distribution);
83
							}
84
						}else if (el.getFeature().equals(Feature.DISTRIBUTION())){
85
							//TODO
86
							String message = "Distribution export for TextData not yet implemented";
87
							logger.warn(message);
88
						}
89
					}
90
				}
91

  
92
                flushWriter(state, file);
74
				handleTaxonNode(state, node);
93 75
			}
94 76
        } catch (Exception e) {
95 77
            String message = "Unexpected exception " + e.getMessage();
96 78
            state.getResult().addException(e, message, "DwcaDistributionExport.doInvoke()");
97 79
		}finally {
98
			closeWriter(file, state);
80
			closeWriter(state);
99 81
		}
100 82
		commitTransaction(txStatus);
101 83
		return;
102 84
	}
103 85

  
86
    /**
87
     * @param state
88
     * @param node
89
     * @throws IOException
90
     * @throws FileNotFoundException
91
     * @throws UnsupportedEncodingException
92
     */
93
    protected void handleTaxonNode(DwcaTaxExportState state,  TaxonNode node)
94
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
95

  
96
        try {
97
            DwcaTaxExportConfigurator config = state.getConfig();
98

  
99
            Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
100

  
101
            Set<TaxonDescription> descriptions = taxon.getDescriptions();
102
            for (TaxonDescription description : descriptions){
103
            	for (DescriptionElementBase el : description.getElements()){
104
            		if (el.isInstanceOf(Distribution.class) ){
105
            			if (! state.recordExists(file, el)){
106
            				DwcaDistributionRecord record = new DwcaDistributionRecord(metaRecord, config);
107
            				Distribution distribution = CdmBase.deproxy(el, Distribution.class);
108
            				handleDistribution(record, distribution, taxon, config);
109
            				PrintWriter writer = createPrintWriter(state, file);
110
            	            record.write(state, writer);
111
            				state.addExistingRecord(file, distribution);
112
            			}
113
            		}else if (el.getFeature().equals(Feature.DISTRIBUTION())){
114
            		    String message = "Distribution export for TextData not yet implemented";
115
            			state.getResult().addWarning(message);
116
            		}
117
            	}
118
            }
119
        } catch (Exception e) {
120
            String message = "Unexpected exception: " + e.getMessage();
121
            state.getResult().addException(e, message);
122
        }finally{
123
            flushWriter(state, file);
124
        }
125
    }
126

  
104 127

  
105 128

  
106 129

  
......
134 157
		return result;
135 158
	}
136 159

  
137

  
138 160
	@Override
139 161
	protected boolean isIgnore(DwcaTaxExportState state) {
140 162
		return ! state.getConfig().isDoDistributions();
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaExportBase.java
64 64

  
65 65
    protected static final boolean IS_CORE = true;
66 66

  
67
    protected DwcaTaxOutputFile file;
68

  
69

  
67 70
    @Autowired
68 71
    private IClassificationService classificationService;
69 72

  
......
316 319
     * @param file
317 320
     * @param state
318 321
     */
319
    protected void closeWriter(DwcaTaxOutputFile file, DwcaTaxExportState state) {
322
    protected void closeWriter(DwcaTaxExportState state) {
320 323
        PrintWriter writer = state.getWriter(file);
321 324
        if (writer != null && state.isZip() == false){
322 325
            writer.close();
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaImageExport.java
9 9

  
10 10
package eu.etaxonomy.cdm.io.dwca.out;
11 11

  
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
12 14
import java.io.PrintWriter;
15
import java.io.UnsupportedEncodingException;
13 16
import java.util.List;
14 17
import java.util.Set;
15 18

  
16 19
import org.apache.log4j.Logger;
17
import org.springframework.stereotype.Component;
18 20
import org.springframework.transaction.TransactionStatus;
19 21

  
20 22
import eu.etaxonomy.cdm.model.common.CdmBase;
......
32 34
 * @author a.mueller
33 35
 * @created 20.04.2011
34 36
 */
35
@Component
36 37
public class DwcaImageExport extends DwcaExportBase {
37 38
    private static final long serialVersionUID = -4997807762779037215L;
38 39

  
......
41 42
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Image";
42 43
	protected static final String fileName = "images.txt";
43 44

  
45
    private DwcaMetaDataRecord metaRecord;
46

  
44 47
	/**
45 48
	 * Constructor
46 49
	 */
47
	public DwcaImageExport() {
50
	public DwcaImageExport(DwcaTaxExportState state) {
48 51
		super();
49 52
		this.ioName = this.getClass().getSimpleName();
53
        metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
54
        state.addMetaRecord(metaRecord);
55
        file = DwcaTaxOutputFile.IMAGE;
50 56
	}
51 57

  
52 58
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
......
59 65
	 */
60 66
	@Override
61 67
	protected void doInvoke(DwcaTaxExportState state){
62
		DwcaTaxExportConfigurator config = state.getConfig();
63
		TransactionStatus txStatus = startTransaction(true);
64 68

  
65
		DwcaTaxOutputFile file = DwcaTaxOutputFile.IMAGE;
69
	    TransactionStatus txStatus = startTransaction(true);
70

  
66 71
		try {
67 72
			DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
68 73
			state.addMetaRecord(metaRecord);
69 74

  
70 75
            List<TaxonNode> allNodes = allNodes(state);
71 76
			for (TaxonNode node : allNodes){
72
				Taxon taxon = CdmBase.deproxy(node.getTaxon());
73
				Set<? extends DescriptionBase<?>> descriptions = taxon.getDescriptions();
74
				for (DescriptionBase<?> description : descriptions){
75
					for (DescriptionElementBase o : description.getElements()){
76
						DescriptionElementBase el = CdmBase.deproxy(o);
77
						if (el.getMedia().size() > 0){
78
							for (Media media: el.getMedia()){
79
								for (MediaRepresentation repr : media.getRepresentations()){
80
									for (MediaRepresentationPart part : repr.getParts()){
81
										if (! state.recordExists(file, part)){
82
											DwcaImageRecord record = new DwcaImageRecord(metaRecord, config);
83
											handleMedia(record, media, repr, part, taxon);
84
											PrintWriter writer = createPrintWriter(state, file);
85
								            record.write(state, writer);
86
											state.addExistingRecord(file,part);
87
										}
88
									}
89
								}
90
							}
91
						}
92
					}
93
				}
94

  
95
                flushWriter(state, file);
77
				handleTaxonNode(state, node);
96 78

  
97 79
			}
98 80
        } catch (Exception e) {
99 81
            String message = "Unexpected exception " + e.getMessage();
100 82
            state.getResult().addException(e, message, "DwcaVernacularExport.doInvoke()");
101 83
		}finally {
102
			closeWriter(file, state);
84
			closeWriter(state);
103 85
		}
104 86

  
105 87
		commitTransaction(txStatus);
106 88
		return;
107 89
	}
108 90

  
91
    /**
92
     * @param state
93
     * @param node
94
     * @throws IOException
95
     * @throws FileNotFoundException
96
     * @throws UnsupportedEncodingException
97
     */
98
    protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
99
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
100
        try {
101
            DwcaTaxExportConfigurator config = state.getConfig();
102
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
103
            Set<? extends DescriptionBase<?>> descriptions = taxon.getDescriptions();
104
            for (DescriptionBase<?> description : descriptions){
105
            	for (DescriptionElementBase o : description.getElements()){
106
            		DescriptionElementBase el = CdmBase.deproxy(o);
107
            		if (el.getMedia().size() > 0){
108
            			for (Media media: el.getMedia()){
109
            				for (MediaRepresentation repr : media.getRepresentations()){
110
            					for (MediaRepresentationPart part : repr.getParts()){
111
            						if (! state.recordExists(file, part)){
112
            							DwcaImageRecord record = new DwcaImageRecord(metaRecord, config);
113
            							handleMedia(state, record, media, repr, part, taxon);
114
            							PrintWriter writer = createPrintWriter(state, file);
115
            				            record.write(state, writer);
116
            							state.addExistingRecord(file,part);
117
            						}
118
            					}
119
            				}
120
            			}
121
            		}
122
            	}
123
            }
124

  
125
        } catch (Exception e) {
126
            String message = "Unexpected exception: " + e.getMessage();
127
            state.getResult().addException(e, message);
128
        }finally{
129
            flushWriter(state, file);
130
        }
131
    }
132

  
109 133

  
110 134

  
111 135

  
112
	private void handleMedia(DwcaImageRecord record, Media media, MediaRepresentation repr, MediaRepresentationPart part, Taxon taxon) {
136
	private void handleMedia(DwcaTaxExportState state, DwcaImageRecord record, Media media, MediaRepresentation repr, MediaRepresentationPart part, Taxon taxon) {
113 137
		record.setId(taxon.getId());
114 138
		record.setUuid(taxon.getUuid());
115 139
		if (part.getUri() == null){
116 140
			String message = "No uri available for media ("+media.getId()+"). URI is required field. Taxon: " + this.getTaxonLogString(taxon);
117
			logger.warn(message);
141
			state.getResult().addWarning(message);
118 142
		}
119 143
		record.setIdentifier(part.getUri());
120 144
		record.setTitle(media.getTitleCache());
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaReferenceExport.java
9 9

  
10 10
package eu.etaxonomy.cdm.io.dwca.out;
11 11

  
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
12 14
import java.io.PrintWriter;
15
import java.io.UnsupportedEncodingException;
13 16
import java.util.List;
14 17

  
15 18
import org.apache.commons.lang.StringUtils;
16 19
import org.apache.log4j.Logger;
17
import org.springframework.stereotype.Component;
18 20
import org.springframework.transaction.TransactionStatus;
19 21

  
20 22
import eu.etaxonomy.cdm.model.common.CdmBase;
......
27 29
 * @author a.mueller
28 30
 * @created 20.04.2011
29 31
 */
30
@Component
31 32
public class DwcaReferenceExport extends DwcaExportBase {
32 33

  
33 34
    private static final long serialVersionUID = -8334741499089219441L;
......
37 38
	protected static final String fileName = "reference.txt";
38 39
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Reference";
39 40

  
41
	private DwcaMetaDataRecord metaRecord;
42

  
40 43
	/**
41 44
	 * Constructor
42 45
	 */
43
	public DwcaReferenceExport() {
46
	public DwcaReferenceExport(DwcaTaxExportState state) {
44 47
		super();
45 48
		this.ioName = this.getClass().getSimpleName();
49
		metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
50
        state.addMetaRecord(metaRecord);
51
        file = DwcaTaxOutputFile.REFERENCE;
46 52
	}
47 53

  
48 54
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
......
55 61
	 */
56 62
	@Override
57 63
	protected void doInvoke(DwcaTaxExportState state){
58
		DwcaTaxExportConfigurator config = state.getConfig();
59 64
		TransactionStatus txStatus = startTransaction(true);
60 65

  
61
		DwcaTaxOutputFile file = DwcaTaxOutputFile.REFERENCE;
62

  
63 66
		try {
64
			DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
65
			state.addMetaRecord(metaRecord);
66 67

  
67 68
            List<TaxonNode> allNodes = allNodes(state);
68 69
			for (TaxonNode node : allNodes){
69 70

  
70
			    //sec
71
				DwcaReferenceRecord record = new DwcaReferenceRecord(metaRecord, config);
72
				Taxon taxon = CdmBase.deproxy(node.getTaxon());
73
				Reference sec = taxon.getSec();
74
				if (sec != null && ! state.recordExists(file, sec)){
75
					handleReference(state, record, sec, taxon);
76
					PrintWriter writer = createPrintWriter(state, file);
77
                    record.write(state, writer);
78
					state.addExistingRecord(file, sec);
79
				}
80

  
81
				//nomRef
82
				record = new DwcaReferenceRecord(metaRecord, config);
83
				INomenclaturalReference nomRefI = taxon.getName().getNomenclaturalReference();
84
				Reference nomRef = CdmBase.deproxy(nomRefI, Reference.class);
85
				if (nomRef != null && ! state.recordExists(file, nomRef)){
86
					handleReference(state, record, nomRef, taxon);
87
					PrintWriter writer = createPrintWriter(state, file);
88
		            record.write(state, writer);
89
					state.addExistingRecord(file, nomRef);
90
				}
91

  
92
                flushWriter(state, file);
71
			    handleTaxonNode(state, node);
93 72

  
94 73
			}
95 74
		} catch (Exception e) {
96 75
	          String message = "Unexpected exception: " + e.getMessage();
97 76
	          state.getResult().addException (e, message, "DwcaReferenceExport.doInvoke()");
98 77
		} finally{
99
			closeWriter(file, state);
78
			closeWriter(state);
100 79
		}
101 80
		commitTransaction(txStatus);
102 81
		return;
103 82
	}
104 83

  
84
    /**
85
     * @param state
86
     * @param metaRecord
87
     * @param node
88
     * @throws IOException
89
     * @throws FileNotFoundException
90
     * @throws UnsupportedEncodingException
91
     */
92
    protected void handleTaxonNode(DwcaTaxExportState state,
93
            TaxonNode node)
94
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
95
        try {
96
            //sec
97
            DwcaReferenceRecord record = new DwcaReferenceRecord(metaRecord, state.getConfig());
98
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
99
            Reference sec = taxon.getSec();
100
            if (sec != null && ! state.recordExists(file, sec)){
101
            	handleReference(state, record, sec, taxon);
102
            	PrintWriter writer = createPrintWriter(state, file);
103
                record.write(state, writer);
104
            	state.addExistingRecord(file, sec);
105
            }
106

  
107
            //nomRef
108
            record = new DwcaReferenceRecord(metaRecord, state.getConfig());
109
            INomenclaturalReference nomRefI = taxon.getName().getNomenclaturalReference();
110
            Reference nomRef = CdmBase.deproxy(nomRefI, Reference.class);
111
            if (nomRef != null && ! state.recordExists(file, nomRef)){
112
            	handleReference(state, record, nomRef, taxon);
113
            	PrintWriter writer = createPrintWriter(state, file);
114
                record.write(state, writer);
115
            	state.addExistingRecord(file, nomRef);
116
            }
117

  
118
        } catch (Exception e) {
119
            String message = "Unexpected exception: " + e.getMessage();
120
            state.getResult().addException(e, message);
121
        }finally{
122
            flushWriter(state, file);
123
        }
124
    }
125

  
105 126
	private void handleReference(DwcaTaxExportState state, DwcaReferenceRecord record, Reference reference, Taxon taxon) {
106 127

  
107 128
		record.setId(taxon.getId());
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaResourceRelationExport.java
18 18
import java.util.Set;
19 19

  
20 20
import org.apache.log4j.Logger;
21
import org.springframework.stereotype.Component;
22 21
import org.springframework.transaction.TransactionStatus;
23 22

  
24 23
import eu.etaxonomy.cdm.model.common.CdmBase;
......
44 43
 * @author a.mueller
45 44
 * @created 20.04.2011
46 45
 */
47
@Component
48 46
public class DwcaResourceRelationExport extends DwcaExportBase {
49 47
    private static final long serialVersionUID = 33810773244068812L;
50 48

  
......
53 51
	private static final String ROW_TYPE = "http://rs.tdwg.org/dwc/terms/ResourceRelationship";
54 52
	protected static final String fileName = "resourceRelationship.txt";
55 53

  
54
	private DwcaMetaDataRecord metaRecord;
55

  
56

  
56 57
	/**
57 58
	 * Constructor
58 59
	 */
59
	public DwcaResourceRelationExport() {
60
	public DwcaResourceRelationExport(DwcaTaxExportState state) {
60 61
		super();
61 62
		this.ioName = this.getClass().getSimpleName();
63
	    metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
64
	    state.addMetaRecord(metaRecord);
65
        file = DwcaTaxOutputFile.RESOURCE_RELATION;
62 66
	}
63 67

  
64 68
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
......
83 87
            List<TaxonNode> allNodes = allNodes(state);
84 88

  
85 89
			for (TaxonNode node : allNodes){
86
			    makeSingleTaxonNode(state, node, metaRecord, file);
87
                flushWriter(state,file);
90
			    handleTaxonNode(state, node);
88 91
			}
89 92
		} catch (IOException e) {
90 93
	         String message = "Unexpected exception " + e.getMessage();
91 94
	         state.getResult().addException(e, message, "DwcaResourceRelationExport.doInvoke()");
92 95
		} finally{
93
			closeWriter(file, state);
96
			closeWriter(state);
94 97
		}
95 98
		commitTransaction(txStatus);
96 99
		return;
97 100
	}
98 101

  
99 102

  
100
    private void makeSingleTaxonNode(DwcaTaxExportState state, TaxonNode node, DwcaMetaDataRecord metaRecord,
101
            DwcaTaxOutputFile file) throws FileNotFoundException, UnsupportedEncodingException, IOException{
102
        DwcaTaxExportConfigurator config = state.getConfig();
103

  
104
        Taxon taxon = CdmBase.deproxy(node.getTaxon());
105

  
106
        //taxon interactions
107
        Set<TaxonDescription> descriptions = taxon.getDescriptions();
108
        for (TaxonDescription description : descriptions){
109
            for (DescriptionElementBase el : description.getElements()){
110
                if (el.isInstanceOf(TaxonInteraction.class)){
111
                    DwcaResourceRelationRecord record = new DwcaResourceRelationRecord(metaRecord, config);
112
                    TaxonInteraction taxonInteraction = CdmBase.deproxy(el,TaxonInteraction.class);
113
                    if (! state.recordExistsUuid(taxonInteraction)){
114
                        handleInteraction(record, taxon, taxonInteraction);
115
                        PrintWriter writer = createPrintWriter(state, file);
116
                        record.write(state, writer);
117
                        state.addExistingRecordUuid(taxonInteraction);
103
    protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node) throws FileNotFoundException, UnsupportedEncodingException, IOException{
104

  
105
        try {
106
            DwcaTaxExportConfigurator config = state.getConfig();
107

  
108
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
109

  
110
            //taxon interactions
111
            Set<TaxonDescription> descriptions = taxon.getDescriptions();
112
            for (TaxonDescription description : descriptions){
113
                for (DescriptionElementBase el : description.getElements()){
114
                    if (el.isInstanceOf(TaxonInteraction.class)){
115
                        DwcaResourceRelationRecord record = new DwcaResourceRelationRecord(metaRecord, config);
116
                        TaxonInteraction taxonInteraction = CdmBase.deproxy(el,TaxonInteraction.class);
117
                        if (! state.recordExistsUuid(taxonInteraction)){
118
                            handleInteraction(state, record, taxon, taxonInteraction);
119
                            PrintWriter writer = createPrintWriter(state, file);
120
                            record.write(state, writer);
121
                            state.addExistingRecordUuid(taxonInteraction);
122
                        }
118 123
                    }
119 124
                }
120 125
            }
121
        }
122 126

  
123
        //concept relationships
124
        for (TaxonRelationship rel : taxon.getTaxonRelations()){
125
            DwcaResourceRelationRecord record = new DwcaResourceRelationRecord(metaRecord, config);
126
            IdentifiableEntity<?> subject = rel.getFromTaxon();
127
            IdentifiableEntity<?> object = rel.getToTaxon();
127
            //concept relationships
128
            for (TaxonRelationship rel : taxon.getTaxonRelations()){
129
                DwcaResourceRelationRecord record = new DwcaResourceRelationRecord(metaRecord, config);
130
                IdentifiableEntity<?> subject = rel.getFromTaxon();
131
                IdentifiableEntity<?> object = rel.getToTaxon();
128 132

  
129
            if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) ){
130
                //misapplied names are handled in core (tax)
131
                continue;
132
            }
133
            if (! state.recordExistsUuid(rel)){
134
                handleRelationship(record, subject, object, rel, false);
135
                PrintWriter writer = createPrintWriter(state, file);
136
                record.write(state, writer);
137
                state.addExistingRecordUuid(rel);
138
            }
133
                if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) ){
134
                    //misapplied names are handled in core (tax)
135
                    continue;
136
                }
137
                if (! state.recordExistsUuid(rel)){
138
                    handleRelationship(record, subject, object, rel, false);
139
                    PrintWriter writer = createPrintWriter(state, file);
140
                    record.write(state, writer);
141
                    state.addExistingRecordUuid(rel);
142
                }
139 143

  
140
        }
144
            }
141 145

  
142
        //Name relationship
143
        //TODO
144
        INonViralName name = taxon.getName();
145
        if (name == null){
146
            String message = "There is a taxon node without name: " + node.getId();
147
            state.getResult().addError(message, "DwcaResourceRelationExport.makeSingleTaxonNode");
148
            return;
149
        }
150
        Set<NameRelationship> rels = name.getNameRelations();
151
        for (NameRelationship rel : rels){
152
            DwcaResourceRelationRecord record = new DwcaResourceRelationRecord(metaRecord, config);
153
            IdentifiableEntity<?> subject = CdmBase.deproxy(rel.getFromName());
154
            IdentifiableEntity<?> object = CdmBase.deproxy(rel.getToName());
155
            boolean isInverse = false;
156
            if(subject == name){
157
                subject = taxon;
158
            }else if(object == name){
159
                object= subject;
160
                subject = taxon;
161
                isInverse = true;
162
            }else{
163
                String message = "Both, subject and object, are not part of the relationship for " + name.getTitleCache();
164
                logger.warn(message);
165
                state.getResult().addWarning(message, "DwcaResourceRelationExport.makeSingleTaxonNode");
146
            //Name relationship
147
            //TODO
148
            INonViralName name = taxon.getName();
149
            if (name == null){
150
                String message = "There is a taxon node without name: " + node.getId();
151
                state.getResult().addError(message, "DwcaResourceRelationExport.makeSingleTaxonNode");
152
                return;
166 153
            }
154
            Set<NameRelationship> rels = name.getNameRelations();
155
            for (NameRelationship rel : rels){
156
                DwcaResourceRelationRecord record = new DwcaResourceRelationRecord(metaRecord, config);
157
                IdentifiableEntity<?> subject = CdmBase.deproxy(rel.getFromName());
158
                IdentifiableEntity<?> object = CdmBase.deproxy(rel.getToName());
159
                boolean isInverse = false;
160
                if(subject == name){
161
                    subject = taxon;
162
                }else if(object == name){
163
                    object= subject;
164
                    subject = taxon;
165
                    isInverse = true;
166
                }else{
167
                    String message = "Both, subject and object, are not part of the relationship for " + name.getTitleCache();
168
                    state.getResult().addWarning(message, "DwcaResourceRelationExport.makeSingleTaxonNode");
169
                }
167 170

  
168
            if (! state.recordExistsUuid(rel)){
169
                //????
170
                handleRelationship(record, subject, object, rel, isInverse);
171
                PrintWriter writer = createPrintWriter(state, file);
172
                record.write(state, writer);
173
                state.addExistingRecordUuid(rel);
171
                if (! state.recordExistsUuid(rel)){
172
                    //????
173
                    handleRelationship(record, subject, object, rel, isInverse);
174
                    PrintWriter writer = createPrintWriter(state, file);
175
                    record.write(state, writer);
176
                    state.addExistingRecordUuid(rel);
177
                }
174 178
            }
175 179

  
180
        } catch (Exception e) {
181
            String message = "Unexpected exception: " + e.getMessage();
182
            state.getResult().addException(e, message);
183
        }finally{
184
            flushWriter(state, file);
176 185
        }
186

  
177 187
        return;
178 188
    }
179 189

  
......
212 222

  
213 223

  
214 224

  
215
	private void handleInteraction(DwcaResourceRelationRecord record, IdentifiableEntity<?> subject, TaxonInteraction interaction) {
225
	private void handleInteraction(DwcaTaxExportState state, DwcaResourceRelationRecord record, IdentifiableEntity<?> subject, TaxonInteraction interaction) {
216 226
		Taxon object = interaction.getTaxon2();
227
		if (object == null){
228
		    state.getResult().addWarning("Taxon interaction has no target object.");
229
		}
217 230
		Map<Language, LanguageString> description = interaction.getDescription();
218 231

  
219 232
		record.setId(subject.getId());
220 233
		record.setUuid(subject.getUuid());
221 234

  
222
		record.setRelatedResourceId(object.getUuid());
235
		record.setRelatedResourceId(object == null ? null : object.getUuid());
223 236
		//TODO transform to controlled voc
224 237
		if (description != null && description.get(Language.DEFAULT()) != null){
225 238
			record.setRelationshipOfResource(description.get(Language.DEFAULT()).getText());
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaResultProcessor.java
134 134
        ExportResult finalResult = state.getResult();
135 135

  
136 136
        if (!result.isEmpty() ){
137
            //Replace quotes by double quotes
138 137
            for (DwcaTaxOutputFile table: result.keySet()){
139 138
                //write each table in a stream ...
140 139
                Object tableResult = result.get(table);
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxExport.java
58 58
	public DwcaTaxExport() {
59 59
		super();
60 60
		this.ioName = this.getClass().getSimpleName();
61
        file = DwcaTaxOutputFile.TAXON;
61 62
	}
62 63

  
63 64
	/** Retrieves data from a CDM DB and serializes the CDM to XML.
......
71 72
	@Override
72 73
	protected void doInvoke(DwcaTaxExportState state){
73 74

  
74
		DwcaTaxExportConfigurator config = state.getConfig();
75 75
		TransactionStatus txStatus = startTransaction(true);
76 76

  
77 77
		DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(true, fileName, ROW_TYPE);
78 78
		state.addMetaRecord(metaRecord);
79 79

  
80
		DwcaTaxOutputFile file = DwcaTaxOutputFile.TAXON;
80
		DwcaReferenceExport refExport = new DwcaReferenceExport(state);
81
		DwcaResourceRelationExport relationExport = new DwcaResourceRelationExport(state);
82
		DwcaTypesExport typesExport = new DwcaTypesExport(state);
83
		DwcaVernacularExport vernacularExport = new DwcaVernacularExport(state);
84
		DwcaDescriptionExport descriptionExport = new DwcaDescriptionExport(state);
85
		DwcaDistributionExport distributionExport = new DwcaDistributionExport(state);
86
		DwcaImageExport imageExport = new DwcaImageExport(state);
87

  
81 88
		try {
82 89

  
83 90
			List<TaxonNode> allNodes = allNodes(state);
84 91

  
85 92
			for (TaxonNode node : allNodes){
86
				Taxon taxon = CdmBase.deproxy(node.getTaxon());
87
				DwcaTaxRecord record = new DwcaTaxRecord(metaRecord, config);
88

  
89
				TaxonName name = taxon.getName();
90
				Taxon parent = node.getParent() == null ? null : node.getParent().getTaxon();
91
				TaxonName basionym = name.getBasionym();
92
				Classification classification = node.getClassification();
93
				if (! state.recordExists(file, taxon)){
94
					handleTaxonBase(state, record, taxon, name, taxon, parent, basionym, classification, null, false, false);
95
					PrintWriter writer = createPrintWriter(state, file);
96
		            record.write(state, writer);
97
					state.addExistingRecord(file, taxon);
98
				}
99

  
100
				node.getClassification().getName();
101
				//synonyms
102
				handleSynonyms(state, taxon, file, classification, metaRecord);
103

  
104
				//misapplied names
105
				handleMisapplication(state, taxon, file, classification, metaRecord);
106

  
107
				flushWriter(state, file);
108

  
93
				handleTaxonNode(state, metaRecord, node);
94
				refExport.handleTaxonNode(state, node);
95
				relationExport.handleTaxonNode(state, node);
96
				typesExport.handleTaxonNode(state, node);
97
				vernacularExport.handleTaxonNode(state, node);
98
				descriptionExport.handleTaxonNode(state, node);
99
				distributionExport.handleTaxonNode(state, node);
100
				imageExport.handleTaxonNode(state, node);
109 101
			}
110 102
		} catch (Exception e) {
111 103
		    String message = "Unexpected exception: " + e.getMessage();
112 104
			state.getResult().addException(e, message, "DwcaTaxExport.doInvoke()");
113 105
		}
114 106
		finally{
115
			closeWriter(file, state);
107
			closeWriter(state);
108
			refExport.closeWriter(state);
109
			relationExport.closeWriter(state);
110
			typesExport.closeWriter(state);
111
			vernacularExport.closeWriter(state);
112
			descriptionExport.closeWriter(state);
113
			distributionExport.closeWriter(state);
114
			imageExport.closeWriter(state);
116 115
		}
117 116
		commitTransaction(txStatus);
118 117
		return;
119 118

  
120 119
	}
121 120

  
121
    /**
122
     * @param state
123
     * @param metaRecord
124
     * @param node
125
     * @throws IOException
126
     * @throws FileNotFoundException
127
     * @throws UnsupportedEncodingException
128
     */
129
    private void handleTaxonNode(DwcaTaxExportState state,
130
            DwcaMetaDataRecord metaRecord, TaxonNode node)
131
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
132

  
133
        try {
134
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
135
            DwcaTaxExportConfigurator config = state.getConfig();
136
            DwcaTaxRecord record = new DwcaTaxRecord(metaRecord, config);
137

  
138
            TaxonName name = taxon.getName();
139
            Taxon parent = node.getParent() == null ? null : node.getParent().getTaxon();
140
            TaxonName basionym = name.getBasionym();
141
            Classification classification = node.getClassification();
142
            if (! state.recordExists(file, taxon)){
143
            	handleTaxonBase(state, record, taxon, name, taxon, parent, basionym, classification, null, false, false);
144
            	PrintWriter writer = createPrintWriter(state, file);
145
                record.write(state, writer);
146
            	state.addExistingRecord(file, taxon);
147
            }
148

  
149
            node.getClassification().getName();
150
            //synonyms
151
            handleSynonyms(state, taxon, file, classification, metaRecord);
152

  
153
            //misapplied names
154
            handleMisapplication(state, taxon, file, classification, metaRecord);
155
        } catch (Exception e) {
156
            String message = "Unexpected exception: " + e.getMessage();
157
            state.getResult().addException(e, message);
158
        }finally{
159
            flushWriter(state, file);
160
        }
161

  
162
    }
163

  
122 164

  
123 165

  
124 166
	private void handleSynonyms(DwcaTaxExportState state, Taxon taxon, DwcaTaxOutputFile file,
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxExportConfigurator.java
92 92
	protected void makeIoClassList() {
93 93
		ioClassList = new Class[] {
94 94
				DwcaTaxExport.class
95
				,DwcaResourceRelationExport.class
96
				,DwcaTypesExport.class
97
				,DwcaVernacularExport.class
98
				,DwcaReferenceExport.class
99
				,DwcaDescriptionExport.class
100
				,DwcaDistributionExport.class
101
				,DwcaImageExport.class
95
//				,DwcaResourceRelationExport.class
96
//				,DwcaTypesExport.class
97
//				,DwcaVernacularExport.class
98
//				,DwcaReferenceExport.class
99
//				,DwcaDescriptionExport.class
100
//				,DwcaDistributionExport.class
101
//				,DwcaImageExport.class
102 102
				,DwcaMetaDataExport.class
103 103
				,DwcaEmlExport.class
104 104
				,DwcaZipExport.class
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTypesExport.java
17 17
import java.util.Set;
18 18

  
19 19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21 20
import org.springframework.transaction.TransactionStatus;
22 21

  
23 22
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
......
44 43
 * @author a.mueller
45 44
 * @created 20.04.2011
46 45
 */
47
@Component
48 46
public class DwcaTypesExport extends DwcaExportBase {
49 47

  
50 48
    private static final long serialVersionUID = 8879154738843628476L;
......
54 52
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/TypesAndSpecimen";
55 53
	protected static final String fileName = "typesAndSpecimen.txt";
56 54

  
55
	private DwcaMetaDataRecord metaRecord;
56

  
57

  
57 58
	/**
58 59
	 * Constructor
59 60
	 */
60
	public DwcaTypesExport() {
61
	public DwcaTypesExport(DwcaTaxExportState state) {
61 62
		super();
62 63
		this.ioName = this.getClass().getSimpleName();
64
        metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
65
        state.addMetaRecord(metaRecord);
66
        file = DwcaTaxOutputFile.TYPES;
63 67
	}
64 68

  
65 69
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
......
72 76
	 */
73 77
	@Override
74 78
	protected void doInvoke(DwcaTaxExportState state){
75
		DwcaTaxExportConfigurator config = state.getConfig();
76 79
		TransactionStatus txStatus = startTransaction(true);
77 80

  
78 81
		DwcaTaxOutputFile file = DwcaTaxOutputFile.TYPES;
......
85 88
            List<TaxonNode> allNodes = state.getAllNodes();
86 89

  
87 90
			for (TaxonNode node : allNodes){
88
				Taxon taxon = CdmBase.deproxy(node.getTaxon());
89

  
90
				//TODO use API methods to retrieve all related specimen
91

  
92
				//individual associations
93
				Set<TaxonDescription> descriptions = taxon.getDescriptions();
94
				for (TaxonDescription description : descriptions){
95
					for (DescriptionElementBase el : description.getElements()){
96
						if (el.isInstanceOf(IndividualsAssociation.class)){
97
							DwcaTypesRecord record = new DwcaTypesRecord(metaRecord, config);
98
							IndividualsAssociation individualAssociation = CdmBase.deproxy(el,IndividualsAssociation.class);
99
							if (! state.recordExistsUuid(individualAssociation)
100
							        && handleSpecimen(state, record, individualAssociation, null, taxon, config)){
101
							    PrintWriter writer = createPrintWriter(state, file);
102
					            record.write(state, writer);
103
								state.addExistingRecordUuid(individualAssociation);
104
							}
105
						}
106
					}
107
				}
108

  
109
				//type specimen
110
				INonViralName nvn = taxon.getName();
111
				handleTypeName(state, file, taxon, nvn, metaRecord);
112
				for (Synonym synonym : taxon.getSynonyms()){
113
					handleTypeName(state, file, synonym, nvn, metaRecord);
114
				}
115

  
116
				//FIXME
117
				//Determinations
118

  
119
				flushWriter(state, file);
91
				handleTaxonNode(state, node);
120 92

  
121 93
			}
122 94
		} catch (Exception e) {
123 95
	          String message = "Unexpected exception " + e.getMessage();
124 96
	          state.getResult().addException(e, message, "DwcaTypesExport.doInvoke()");
125 97
		} finally{
126
			closeWriter(file, state);
98
			closeWriter(state);
127 99
		}
128 100
		commitTransaction(txStatus);
129 101
		return;
130 102
	}
131 103

  
104
    /**
105
     * @param state
106
     * @param node
107
     * @throws IOException
108
     * @throws FileNotFoundException
109
     * @throws UnsupportedEncodingException
110
     */
111
    protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
112
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
113

  
114
        try {
115
            DwcaTaxExportConfigurator config = state.getConfig();
116

  
117
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
118

  
119
            //TODO use API methods to retrieve all related specimen
120

  
121
            //individual associations
122
            Set<TaxonDescription> descriptions = taxon.getDescriptions();
123
            for (TaxonDescription description : descriptions){
124
            	for (DescriptionElementBase el : description.getElements()){
125
            		if (el.isInstanceOf(IndividualsAssociation.class)){
126
            			DwcaTypesRecord record = new DwcaTypesRecord(metaRecord, config);
127
            			IndividualsAssociation individualAssociation = CdmBase.deproxy(el,IndividualsAssociation.class);
128
            			if (! state.recordExistsUuid(individualAssociation)
129
            			        && handleSpecimen(state, record, individualAssociation, null, taxon, config)){
130
            			    PrintWriter writer = createPrintWriter(state, file);
131
            	            record.write(state, writer);
132
            				state.addExistingRecordUuid(individualAssociation);
133
            			}
134
            		}
135
            	}
136
            }
137

  
138
            //type specimen
139
            INonViralName nvn = taxon.getName();
140
            handleTypeName(state, file, taxon, nvn, metaRecord);
141
            for (Synonym synonym : taxon.getSynonyms()){
142
            	handleTypeName(state, file, synonym, nvn, metaRecord);
143
            }
144

  
145
            //FIXME
146
            //Determinations
147

  
148
        } catch (Exception e) {
149
            String message = "Unexpected exception: " + e.getMessage();
150
            state.getResult().addException(e, message);
151
        }finally{
152
            flushWriter(state, file);
153
        }
154
    }
155

  
132 156
	/**
133 157
	 * @param state
134 158
	 * @param writer
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaVernacularExport.java
9 9

  
10 10
package eu.etaxonomy.cdm.io.dwca.out;
11 11

  
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
12 14
import java.io.PrintWriter;
15
import java.io.UnsupportedEncodingException;
13 16
import java.util.List;
14 17
import java.util.Set;
15 18

  
16 19
import org.apache.commons.lang.StringUtils;
17 20
import org.apache.log4j.Logger;
18
import org.springframework.stereotype.Component;
19 21
import org.springframework.transaction.TransactionStatus;
20 22

  
21 23
import eu.etaxonomy.cdm.model.common.CdmBase;
......
30 32
 * @author a.mueller
31 33
 * @created 18.04.2011
32 34
 */
33
@Component
34 35
public class DwcaVernacularExport extends DwcaExportBase {
35 36
    private static final long serialVersionUID = 3169086545830374918L;
36 37

  
......
39 40
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/VernacularName";
40 41
	protected static final String fileName = "vernacular.txt";
41 42

  
43
    private DwcaMetaDataRecord metaRecord;
42 44

  
43 45
	/**
44 46
	 * Constructor
45 47
	 */
46
	public DwcaVernacularExport() {
48
	public DwcaVernacularExport(DwcaTaxExportState state) {
47 49
		super();
48 50
		this.ioName = this.getClass().getSimpleName();
51
        metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
52
        state.addMetaRecord(metaRecord);
53
        file = DwcaTaxOutputFile.VERNACULAR;
49 54
	}
50 55

  
51 56
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
......
58 63
	 */
59 64
	@Override
60 65
	protected void doInvoke(DwcaTaxExportState state){
61
		DwcaTaxExportConfigurator config = state.getConfig();
62 66
		TransactionStatus txStatus = startTransaction(true);
63 67

  
64
		DwcaTaxOutputFile file = DwcaTaxOutputFile.VERNACULAR;
65 68
		try {
66 69

  
67 70
			DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
......
69 72

  
70 73
            List<TaxonNode> allNodes = allNodes(state);
71 74
			for (TaxonNode node : allNodes){
72
				Taxon taxon = CdmBase.deproxy(node.getTaxon());
73
				Set<TaxonDescription> descriptions = taxon.getDescriptions();
74
				for (TaxonDescription description : descriptions){
75
					for (DescriptionElementBase el : description.getElements()){
76
						if (el.isInstanceOf(CommonTaxonName.class)){
77
							DwcaVernacularRecord record = new DwcaVernacularRecord(metaRecord, config);
78
							CommonTaxonName commonTaxonName = CdmBase.deproxy(el, CommonTaxonName.class);
79
							if (! state.recordExists(file, commonTaxonName)){
80
								handleCommonTaxonName(record, commonTaxonName, taxon, config);
81
								PrintWriter writer = createPrintWriter(state, file);
82
								record.write(state, writer);
83
								state.addExistingRecord(file, commonTaxonName);
84
							}
85
						}else if (el.getFeature().equals(Feature.COMMON_NAME())){
86
							//TODO
87
							String message = "Vernacular name export for TextData not yet implemented";
88
							state.getResult().addError(message, this, "doInvoke()");
89
							logger.warn(message);
90
						}
91
					}
92
				}
93

  
94
                flushWriter(state, file);
75
				handleTaxonNode(state, node);
95 76
			}
96 77
		} catch (Exception e) {
97 78
	         String message = "Unexpected exception " + e.getMessage();
98 79
	         state.getResult().addException(e, message, "DwcaVernacularExport.doInvoke()");
99 80
		} finally{
100
			closeWriter(file, state);
81
			closeWriter(state);
101 82
		}
102 83
		commitTransaction(txStatus);
103 84
		return;
104 85
	}
105 86

  
87
    /**
88
     * @param state
89
     * @param config
90
     * @param file
91
     * @param metaRecord
92
     * @param node
93
     * @throws IOException
94
     * @throws FileNotFoundException
95
     * @throws UnsupportedEncodingException
96
     */
97
    protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
98
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
99

  
100
        try {
101
            DwcaTaxExportConfigurator config = state.getConfig();
102
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
103
            Set<TaxonDescription> descriptions = taxon.getDescriptions();
104
            for (TaxonDescription description : descriptions){
105
            	for (DescriptionElementBase el : description.getElements()){
106
            		if (el.isInstanceOf(CommonTaxonName.class)){
107
            			DwcaVernacularRecord record = new DwcaVernacularRecord(metaRecord, config);
108
            			CommonTaxonName commonTaxonName = CdmBase.deproxy(el, CommonTaxonName.class);
109
            			if (! state.recordExists(file, commonTaxonName)){
110
            				handleCommonTaxonName(record, commonTaxonName, taxon, config);
111
            				PrintWriter writer = createPrintWriter(state, file);
112
            				record.write(state, writer);
113
            				state.addExistingRecord(file, commonTaxonName);
114
            			}
115
            		}else if (el.getFeature().equals(Feature.COMMON_NAME())){
116
            			//TODO
117
            			String message = "Vernacular name export for TextData not yet implemented";
118
            			state.getResult().addError(message, this, "doInvoke()");
119
            			logger.warn(message);
120
            		}
121
            	}
122
            }
123

  
124
        } catch (Exception e) {
125
            String message = "Unexpected exception: " + e.getMessage();
126
            state.getResult().addException(e, message);
127
        }finally{
128
            flushWriter(state, file);
129
        }
130
    }
131

  
106 132

  
107 133

  
108 134

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)