Project

General

Profile

Download (6.53 KB) Statistics
| Branch: | Tag: | Revision:
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
package eu.etaxonomy.taxeditor.bulkeditor.input;
10

    
11
import java.util.ArrayList;
12
import java.util.Collections;
13
import java.util.Comparator;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.UUID;
17

    
18
import org.eclipse.jface.resource.ImageDescriptor;
19
import org.eclipse.ui.IEditorInput;
20
import org.eclipse.ui.IPersistableElement;
21

    
22
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
23
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
24
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.common.ICdmBase;
27
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
28
import eu.etaxonomy.cdm.model.common.MarkerType;
29
import eu.etaxonomy.cdm.strategy.merge.IMergable;
30
import eu.etaxonomy.cdm.strategy.merge.MergeException;
31
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
32
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
33
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
34
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
35
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.CdmBaseSortProvider;
36
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.TitleCacheComparator;
37
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
38
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
39
import eu.etaxonomy.taxeditor.model.MessagingUtils;
40
import eu.etaxonomy.taxeditor.store.CdmStore;
41

    
42
/**
43
 * @author p.ciardelli
44
 * @created 25.06.2009
45
 * @version 1.0
46
 * @param <T>
47
 */
48
public abstract class AbstractBulkEditorInput<T extends ICdmBase> extends CdmEntitySessionInput implements IEditorInput ,
49
    IEntityPersistenceService<T> {
50

    
51
	private UUID entityUuid;
52

    
53
	private List<T> model;
54

    
55
	private IEntityCreator<T> entityCreator;
56
	private final ConversationHolder conversation;
57

    
58
	public AbstractBulkEditorInput() {
59
	    super(true);
60
	    this.conversation = CdmStore.createConversation();
61
	}
62

    
63
	static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
64

    
65
		return BulkEditorInputType.getInput(inputType);
66
	}
67

    
68
	public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
69

    
70
		BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
71

    
72
		AbstractBulkEditorInput editorInput = NewInstance(inputType);
73

    
74
		editorInput.setEntityUuid(entity.getUuid());
75

    
76
		return editorInput;
77
	}
78

    
79
	protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
80

    
81
	protected abstract T loadEntity(UUID entityUuid);
82

    
83
	private void setEntityUuid(UUID entityUuid){
84
		this.entityUuid = entityUuid;
85
	}
86

    
87
	public UUID getEntityUuid() {
88
		return entityUuid;
89
	}
90

    
91
	@Override
92
    public boolean exists() {
93
		// TODO Auto-generated method stub
94
		return false;
95
	}
96

    
97
	@Override
98
    public ImageDescriptor getImageDescriptor() {
99
		// TODO Auto-generated method stub
100
		return null;
101
	}
102

    
103
	@Override
104
    public IPersistableElement getPersistable() {
105
		return null;
106
	}
107

    
108
	/** {@inheritDoc} */
109
	@Override
110
    @SuppressWarnings("unchecked")
111
	public Object getAdapter(Class adapter) {
112
		return null;
113
	}
114

    
115
	public void performSearch(final BulkEditorQuery bulkEditorQuery) {
116

    
117
		List<T> entityList = new ArrayList<T>();
118

    
119
		if(getEntityUuid() != null){
120

    
121
			T entity = loadEntity(getEntityUuid());
122
			entityList.add(entity);
123
			model = entityList;
124
		}
125
		else if(bulkEditorQuery != null){
126

    
127
			IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
128
			Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
129

    
130
			entityList = listEntities(configurator);
131

    
132
			Collections.sort(entityList, queryComparator);
133

    
134

    
135
		}
136

    
137
		model = entityList;
138
	}
139

    
140
	public boolean isMergingEnabled() {
141
		return false;
142
	}
143

    
144
	public boolean isConvertingEnabled() {
145
		return false;
146
	}
147

    
148
	public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
149
		return false;
150
	}
151

    
152

    
153
	/** {@inheritDoc} */
154
	@Override
155
    public boolean merge(T entity, T mergeTarget) {
156
		if (entity instanceof IMergable) {
157
			try {
158
				CdmStore.getCommonService().merge(mergeTarget.getUuid(), entity.getUuid(), (Class<? extends CdmBase>)entity.getClass());
159
			} catch (MergeException e) {
160
				MessagingUtils.errorDialog("Bulk Editor Merge Error",
161
						this,
162
						"Could not merge chosen objects of type " + entity.getClass().getName(),
163
						TaxeditorBulkeditorPlugin.PLUGIN_ID,
164
						e,
165
						true);
166
			}
167
		}
168
		return true;
169
	}
170

    
171

    
172
	/** {@inheritDoc} */
173
	@Override
174
    public T create(T entity) {
175
		return save(entity);
176
	}
177

    
178
	public IEntityCreator<T> getEntityCreator(){
179
		if(entityCreator == null){
180
			entityCreator = createEntityCreator();
181
		}
182
		return entityCreator;
183
	}
184

    
185
	protected abstract IEntityCreator<T> createEntityCreator();
186

    
187
	/**
188
	 * The default implementation returns an empty list of sort providers.
189
	 * @return
190
	 */
191
	public List<IBulkEditorSortProvider<T>> getSortProviders(){
192
		List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
193

    
194
		sortProviders.add(new CdmBaseSortProvider<T>());
195

    
196
		return sortProviders;
197
	}
198

    
199
	/**
200
	 * Returns a textual representation given object. The default implementation
201
	 * in the abstract base class returns the simple name of the class, this may
202
	 * be overwritten to something more specific in subclasses.
203
	 *
204
	 * @param entity
205
	 * @return a textual representation given object.
206
	 */
207
	public String getTypeText(Object entity){
208
		return entity.getClass().getSimpleName();
209
	}
210

    
211
	public String getText(T entity) {
212
		if(entity instanceof IdentifiableEntity){
213
			IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
214

    
215
			return identifiableEntity.getTitleCache();
216
		}
217

    
218
		return "No text. Implement in subclass";
219
	}
220

    
221
	public List<T> getModel() {
222
		return model;
223
	}
224

    
225
	protected boolean replaceInModel(T entity) {
226
	    int index = model.indexOf(entity);
227
	    if(index >= 0) {
228
	        model.set(index, entity);
229
	        return true;
230
	    } else {
231
	        return false;
232
	    }
233
	}
234

    
235
    @Override
236
    public   List<T> getRootEntities() {
237
        return getModel();
238
    }
239

    
240

    
241
    @Override
242
    public Map<Object, List<String>> getPropertyPathsMap() {
243
        // TODO Auto-generated method stub
244
        return null;
245
    }
246

    
247
	public ConversationHolder getConversation() {
248
		return conversation;
249
	}
250
}
(1-1/11)