Project

General

Profile

Download (4.33 KB) Statistics
| Branch: | Tag: | Revision:
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.scripts;
12

    
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
20
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
21
import eu.etaxonomy.cdm.api.service.IDescriptionService;
22
import eu.etaxonomy.cdm.common.AccountStore;
23
import eu.etaxonomy.cdm.database.CdmDataSource;
24
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
25
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
26
import eu.etaxonomy.cdm.database.ICdmDataSource;
27
import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
28
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
29
import eu.etaxonomy.cdm.model.description.Feature;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
31
import eu.etaxonomy.cdm.model.description.TextData;
32
import eu.etaxonomy.cdm.model.media.Media;
33

    
34
/**
35
 * We decided by convention to have only ONE TextData element in an image gallery and that
36
 * all media objects should be attached to that element.
37
 *
38
 * Run this script to clean-up a database for which this convention was not satisfied.
39
 *
40
 * @author n.hoffmann
41
 * @created Jun 9, 2010
42
 * @version 1.0
43
 */
44
public class FixMultipleTextDataInImageGalleries {
45
	public static final Logger logger = Logger.getLogger(FixMultipleTextDataInImageGalleries.class);
46

    
47
	public static ICdmDataSource dataSource(){
48
		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
49
		String cdmServer = "<IP adress>";
50
		String cdmDB = "<database name>";
51
		String cdmUserName = "<user>";
52
		return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
53
	}
54

    
55
	private static ICdmDataSource makeDestination(DatabaseTypeEnum dbType, String cdmServer, String cdmDB, int port, String cdmUserName, String pwd ){
56
		//establish connection
57
		pwd = AccountStore.readOrStorePassword(cdmServer, cdmDB, cdmUserName, pwd);
58
		ICdmDataSource destination;
59
		if(dbType.equals(DatabaseTypeEnum.MySQL)){
60
			destination = CdmDataSource.NewMySqlInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
61
		} else if(dbType.equals(DatabaseTypeEnum.PostgreSQL)){
62
			destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB, port, cdmUserName, pwd);
63
		} else {
64
			//TODO others
65
			throw new RuntimeException("Unsupported DatabaseType");
66
		}
67
		return destination;
68
	}
69

    
70
	/**
71
	 * @param args
72
	 * @throws TermNotFoundException
73
	 * @throws DataSourceNotFoundException
74
	 */
75
	public static void main(String[] args) throws DataSourceNotFoundException, TermNotFoundException {
76

    
77
		CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
78

    
79
		ConversationHolder conversation = applicationController.NewConversation();
80
		conversation.startTransaction();
81

    
82
		IDescriptionService service = applicationController.getDescriptionService();
83

    
84
		// get all taxon descriptions
85
		List<TaxonDescription> result = service.list(TaxonDescription.class, null, null, null, null);
86

    
87
		int countTaxonDescriptions = 0;
88

    
89
		for (TaxonDescription description : result){
90
			// filter image galleries with more than one element
91
			if(description.isImageGallery() && description.getElements().size() > 1){
92
				countTaxonDescriptions++;
93

    
94
				logger.warn("Found image gallery with mulitple TextData: " + description.getElements().size());
95

    
96
				TextData newDescriptionElement = TextData.NewInstance(Feature.IMAGE());
97

    
98
				Set<DescriptionElementBase> elementsToRemove = new HashSet<DescriptionElementBase>();
99

    
100
				// consolidate media from all elements into a new element
101
				for(DescriptionElementBase element : description.getElements()){
102
					List<Media> medias = element.getMedia();
103

    
104
					for(Media media : medias){
105
						newDescriptionElement.addMedia(media);
106
						logger.warn("Consolidating media");
107
					}
108
					elementsToRemove.add(element);
109
				}
110

    
111
				// remove old elements
112
				for(DescriptionElementBase element : elementsToRemove){
113
					description.removeElement(element);
114
				}
115

    
116
				// add the new element
117
				description.addElement(newDescriptionElement);
118

    
119
			}
120
		}
121
		conversation.commit(false);
122

    
123
		logger.warn("Descriptions Processed: "  + countTaxonDescriptions);
124
	}
125
}
    (1-1/1)