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.scripts;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
19
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
20
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
21
|
import eu.etaxonomy.cdm.common.AccountStore;
|
22
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
23
|
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
|
24
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
25
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
26
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
27
|
import eu.etaxonomy.cdm.model.description.Feature;
|
28
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
29
|
import eu.etaxonomy.cdm.model.description.TextData;
|
30
|
import eu.etaxonomy.cdm.model.media.Media;
|
31
|
import eu.etaxonomy.cdm.model.term.init.TermNotFoundException;
|
32
|
|
33
|
/**
|
34
|
* We decided by convention to have only ONE TextData element in an image gallery and that
|
35
|
* all media objects should be attached to that element.
|
36
|
*
|
37
|
* Run this script to clean-up a database for which this convention was not satisfied.
|
38
|
*
|
39
|
* @author n.hoffmann
|
40
|
* @since Jun 9, 2010
|
41
|
* @version 1.0
|
42
|
*/
|
43
|
public class FixMultipleTextDataInImageGalleries {
|
44
|
public static final Logger logger = Logger.getLogger(FixMultipleTextDataInImageGalleries.class);
|
45
|
|
46
|
public static ICdmDataSource dataSource(){
|
47
|
DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
|
48
|
String cdmServer = "<IP adress>";
|
49
|
String cdmDB = "<database name>";
|
50
|
String cdmUserName = "<user>";
|
51
|
return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
|
52
|
}
|
53
|
|
54
|
private static ICdmDataSource makeDestination(DatabaseTypeEnum dbType, String cdmServer, String cdmDB, int port, String cdmUserName, String pwd ){
|
55
|
//establish connection
|
56
|
pwd = AccountStore.readOrStorePassword(cdmServer, cdmDB, cdmUserName, pwd);
|
57
|
ICdmDataSource destination;
|
58
|
if(dbType.equals(DatabaseTypeEnum.MySQL)){
|
59
|
destination = CdmDataSource.NewMySqlInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
|
60
|
} else if(dbType.equals(DatabaseTypeEnum.PostgreSQL)){
|
61
|
destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
|
62
|
} else {
|
63
|
//TODO others
|
64
|
throw new RuntimeException("Unsupported DatabaseType");
|
65
|
}
|
66
|
return destination;
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* @param args
|
71
|
* @throws TermNotFoundException
|
72
|
* @throws DataSourceNotFoundException
|
73
|
*/
|
74
|
public static void main(String[] args) throws DataSourceNotFoundException, TermNotFoundException {
|
75
|
|
76
|
CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
|
77
|
|
78
|
ConversationHolder conversation = applicationController.NewConversation();
|
79
|
conversation.startTransaction();
|
80
|
|
81
|
IDescriptionService service = applicationController.getDescriptionService();
|
82
|
|
83
|
// get all taxon descriptions
|
84
|
List<TaxonDescription> result = service.list(TaxonDescription.class, null, null, null, null);
|
85
|
|
86
|
int countTaxonDescriptions = 0;
|
87
|
|
88
|
for (TaxonDescription description : result){
|
89
|
// filter image galleries with more than one element
|
90
|
if(description.isImageGallery() && description.getElements().size() > 1){
|
91
|
countTaxonDescriptions++;
|
92
|
|
93
|
logger.warn("Found image gallery with mulitple TextData: " + description.getElements().size());
|
94
|
|
95
|
TextData newDescriptionElement = TextData.NewInstance(Feature.IMAGE());
|
96
|
|
97
|
Set<DescriptionElementBase> elementsToRemove = new HashSet<DescriptionElementBase>();
|
98
|
|
99
|
// consolidate media from all elements into a new element
|
100
|
for(DescriptionElementBase element : description.getElements()){
|
101
|
List<Media> medias = element.getMedia();
|
102
|
|
103
|
for(Media media : medias){
|
104
|
newDescriptionElement.addMedia(media);
|
105
|
logger.warn("Consolidating media");
|
106
|
}
|
107
|
elementsToRemove.add(element);
|
108
|
}
|
109
|
|
110
|
// remove old elements
|
111
|
for(DescriptionElementBase element : elementsToRemove){
|
112
|
description.removeElement(element);
|
113
|
}
|
114
|
|
115
|
// add the new element
|
116
|
description.addElement(newDescriptionElement);
|
117
|
|
118
|
}
|
119
|
}
|
120
|
conversation.commit(false);
|
121
|
|
122
|
logger.warn("Descriptions Processed: " + countTaxonDescriptions);
|
123
|
}
|
124
|
}
|