Project

General

Profile

« Previous | Next » 

Revision b2063ede

Added by Patrick Plitzner almost 6 years ago

ref #5458 Cleanup and stability for distribution editor

  • Extract string constants for DB columns
  • renaming methods and variables
  • fix session handling for CRUD operations on NamedAreas

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/presenter/dbstatus/DistributionTablePresenter.java
34 34
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
35 35
import eu.etaxonomy.cdm.model.description.TaxonDescription;
36 36
import eu.etaxonomy.cdm.model.location.NamedArea;
37
import eu.etaxonomy.cdm.model.taxon.Classification;
38 37
import eu.etaxonomy.cdm.model.taxon.Taxon;
39 38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40 39
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
......
70 69

  
71 70

  
72 71
	@Override
73
    public int updateDistributionField(String distributionArea, Object comboValue, Taxon taxon) {
72
    public int updateDistributionField(String distributionAreaString, Object comboValue, Taxon taxon) {
74 73
	    Set<DefinedTermBase> chosenTerms = getChosenTerms();
75
	    NamedArea nArea = null;
76
	    for(DefinedTermBase dt:chosenTerms){
77
	        if(dt.getTitleCache().equalsIgnoreCase(distributionArea)){
78
	            nArea = (NamedArea) dt;
74
	    NamedArea namedArea = null;
75
	    for(DefinedTermBase term:chosenTerms){
76
	        if(term.getTitleCache().equalsIgnoreCase(distributionAreaString)){
77
	            namedArea = (NamedArea) term;
79 78
	            break;
80 79
	        }
81 80
	    }
82
	    List<Distribution> distribution = getDistribution(taxon);
83
	    Distribution db = null;
84
	    for(Distribution dist : distribution){
85
	        if(dist.getArea().equals(nArea)){
86
	            db = dist;
81
	    List<Distribution> distributions = getDistributions(taxon);
82
	    Distribution distribution = null;
83
	    for(Distribution dist : distributions){
84
	        if(dist.getArea().equals(namedArea)){
85
	            distribution = dist;
87 86
	            break;
88 87
	        }
89 88
	    }
90
	    if(comboValue == null){//delete descriptionElementBase
91
	        getDescriptionService().deleteDescriptionElement(db);//descriptionElementbase
89
	    if(distribution==null){
90
	    	//create new distribution
91
	    	distribution = Distribution.NewInstance(namedArea, (PresenceAbsenceTerm) comboValue);
92
			Set<TaxonDescription> descriptions = taxon.getDescriptions();
93
			if (descriptions != null) {
94
			    for (TaxonDescription desc : descriptions) {
95
			        // add to first taxon description
96
			        desc.addElement(distribution);
97
				    getTaxonService().saveOrUpdate(taxon);
98
			        return 0;
99
			    }
100
			} else {// there are no TaxonDescription yet.
101
			    TaxonDescription taxonDescription = TaxonDescription.NewInstance(taxon);
102
			    taxonDescription.addElement(distribution);
103
			    taxon.addDescription(taxonDescription);
104
			    getTaxonService().saveOrUpdate(taxon);
105
			    return 0;
106
			}
107
	    }
108
	    else if(comboValue == null){//delete descriptionElementBase
109
	    	distribution.getInDescription().removeElement(distribution);
92 110
            getTaxonService().saveOrUpdate(taxon);
93 111
            return 1;
94
        }else{
95
           db.setStatus((PresenceAbsenceTerm)comboValue);
96
           getDescriptionService().saveDescriptionElement(db);//descriptionElementbase?
112
	    }
113
	    else{
114
           distribution.setStatus((PresenceAbsenceTerm)comboValue);
115
           getTaxonService().saveOrUpdate(taxon);
97 116
           return 0;
98 117
        }
118
	    return -1;
99 119
	}
100 120

  
101 121
	@Override
......
166 186
	}
167 187

  
168 188
	@Override
169
	public List<Distribution> getDistribution(Taxon taxon) {
189
	public List<Distribution> getDistributions(Taxon taxon) {
170 190
		Set<Feature> setFeature = new HashSet<Feature>(Arrays.asList(Feature.DISTRIBUTION()));
171 191
		List<Distribution> listTaxonDescription = descriptionService.listDescriptionElementsForTaxon(taxon, setFeature, null, null, null, DESCRIPTION_INIT_STRATEGY);
172 192
		return listTaxonDescription;
......
250 270
	@Override
251 271
	public List<PresenceAbsenceTerm> getPresenceAbsenceTerms() {
252 272
		//TODO Better to use TermType instead of class to get the list
253
		return termService.list(PresenceAbsenceTerm.class, null, null, null, DESCRIPTION_INIT_STRATEGY);
273
		return termService.list(PresenceAbsenceTerm.class, null, null, null, null);
254 274
	}
255 275

  
256 276
	protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
src/main/java/eu/etaxonomy/cdm/vaadin/util/CdmQueryFactory.java
25 25
 */
26 26
public class CdmQueryFactory {
27 27

  
28

  
29
	public static final String RANK_COLUMN = "Rank";
30
	public static final String TAXON_COLUMN = "Taxon";
31
	
28 32
    public static final String ID = "id";
29 33
    public static final String UUID_ID = "uuid";
30 34

  
......
82 86
        		"tb.id, " +
83 87
        		"tb.uuid, " +
84 88
        		"tn.classification_id, " +
85
        		"tb.titleCache AS Taxon, " +
86
        		"dtb2.titleCache AS Rank, ";
89
        		"tb.titleCache AS "+TAXON_COLUMN+", " +
90
        		"dtb2.titleCache AS "+RANK_COLUMN+", ";
87 91

  
88 92
        int count = termList.size();
89 93
        for(String term : termList){
src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionTableView.java
3 3
import java.sql.SQLException;
4 4
import java.util.ArrayList;
5 5
import java.util.Arrays;
6
import java.util.Collection;
6 7
import java.util.List;
7 8
import java.util.UUID;
8 9

  
9
import com.vaadin.data.Item;
10 10
import com.vaadin.data.Property;
11 11
import com.vaadin.data.Property.ValueChangeEvent;
12 12
import com.vaadin.data.Property.ValueChangeListener;
13
import com.vaadin.event.ItemClickEvent;
14
import com.vaadin.event.ItemClickEvent.ItemClickListener;
15 13
import com.vaadin.event.ShortcutAction.KeyCode;
16 14
import com.vaadin.event.ShortcutAction.ModifierKey;
17 15
import com.vaadin.navigator.View;
......
22 20
import com.vaadin.ui.Button.ClickEvent;
23 21
import com.vaadin.ui.Button.ClickListener;
24 22
import com.vaadin.ui.ComboBox;
25
import com.vaadin.ui.Component;
26 23
import com.vaadin.ui.CustomComponent;
27 24
import com.vaadin.ui.Notification;
28
import com.vaadin.ui.PopupView;
29
import com.vaadin.ui.PopupView.PopupVisibilityEvent;
30
import com.vaadin.ui.PopupView.PopupVisibilityListener;
31 25
import com.vaadin.ui.Table;
32 26
import com.vaadin.ui.Table.ColumnGenerator;
33 27
import com.vaadin.ui.Window;
......
36 30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
37 31
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
38 32
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
40 33
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
41 34
import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
42 35
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
36
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
43 37

  
44 38
public class DistributionTableView<E> extends CustomComponent implements IDistributionTableComponent, View{
45 39

  
46
    private static final long serialVersionUID = 1L;
40
    private final class AreaColumnGenerator implements ColumnGenerator {
41
		private static final long serialVersionUID = 1L;
42

  
43
		@Override
44
		public Object generateCell(Table source, Object itemId, Object columnId) {
45
		    Property containerProperty = source.getContainerProperty(itemId, columnId);
46
		    Object value = null;
47
		    if(containerProperty != null){
48
		        value = containerProperty.getValue();
49
		    }
50
		    final UUID uuid = UUID.fromString(table.getItem(itemId).getItemProperty("uuid").getValue().toString());
51
		    final ComboBox box = new ComboBox("Occurrence Status: ", listener.getPresenceAbsenceContainer());
52
		    final String area = columnId.toString();
53
		    box.setImmediate(true);
54
		    box.setBuffered(true);
55
		    box.setValue(compareObjectToPAT(value));
56
		    final Object value1 = value;
57
		    box.addValueChangeListener(new ValueChangeListener() {
58
		        @Override
59
		        public void valueChange(ValueChangeEvent event) {
60
		            Taxon taxon = (Taxon)listener.getTaxonService().load(uuid);
61
		            listener.updateDistributionField(area, box.getValue(), taxon);
62
		        }
63
		    });
64
		    return box;
65
		}
66
	}
67

  
68
	private static final long serialVersionUID = 1L;
47 69
    private HorizontalToolbar toolbar;
48 70
	private Table table;
49 71

  
50 72
	private Taxon currentTaxon;
51 73

  
52
	private ArrayList<Object> propertyList = new ArrayList<Object>();
53

  
54 74
	private DistributionTableComponentListener listener;
55 75

  
56 76
	private List<String> columnList;
......
99 119

  
100 120
    @Override
101 121
    public void enter(ViewChangeEvent event) {
102
        // TODO Auto-generated method stub
103

  
104 122
    }
105 123

  
106

  
107

  
108 124
	public void dataBinding() throws SQLException{
109 125
		CdmSQLContainer container = listener.getSQLContainer();
110 126

  
......
112 128
		table.setColumnReorderingAllowed(true);
113 129
		table.setSortEnabled(true);
114 130

  
115
		columnList = new ArrayList<String>(Arrays.asList(new String[]{"Taxon","Rank"}));
116
		columnList.addAll(listener.getTermList());
131
		columnList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,CdmQueryFactory.RANK_COLUMN}));
132
		List<String> termList = listener.getTermList();
133
		columnList.addAll(termList);
117 134
		Object[] visibleColumns = columnList.toArray();
118 135
		table.setVisibleColumns(visibleColumns);
119 136

  
120
		headerList = new ArrayList<String>(Arrays.asList(new String[]{"Taxon","Rang"}));
137
		headerList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,"Rang"}));
121 138
		headerList.addAll(listener.getAbbreviatedTermList());
122 139

  
123 140
		table.setColumnCollapsingAllowed(true);
124 141
		table.setSelectable(true);
125 142
		table.setPageLength(20);
126 143
		table.setFooterVisible(true);
127
		table.setColumnFooter("Taxon", "Total amount of Taxa displayed: " + container.size());
144
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
128 145

  
129 146
		table.setCacheRate(20);
147
		
148
		//add generated columns for NamedAreas
149
		Collection<?> containerPropertyIds = table.getContainerPropertyIds();
150
		for (Object object : containerPropertyIds) {
151
			if(termList.contains(object)){
152
				  table.addGeneratedColumn(object, new AreaColumnGenerator());
153
			}
154
		}
130 155
	}
131 156

  
132 157

  
......
177 202
				}catch(Exception stateException){
178 203
					//TODO create Table without DTO
179 204
				}
180
				if(propertyList != null){
181
					for(Object propertyId:propertyList){
182
						table.removeGeneratedColumn(propertyId);
183
					}
184
					redrawTable();
185
				}
186 205
				Notification.show("Data saved", Notification.Type.HUMANIZED_MESSAGE);
187
				propertyList = null;
188
				propertyList = new ArrayList<Object>();
189
				table.setEditable(false);
190
				toolbar.getSaveButton().setCaption("Save Data");
191 206
			}
192 207
		});
193 208

  
194

  
195
		//FIXME: Due lack of time needs to be properly done
196

  
197
//		Button editButton = toolbar.getEditButton();
198
//		editButton.setClickShortcut(KeyCode.E, ModifierKey.CTRL);
199
//		editButton.setDescription("Shortcut: CTRL+e");
200
//		editButton.addClickListener(new ClickListener() {
201
//			private static final long serialVersionUID = 1L;
202
//			@Override
203
//			public void buttonClick(ClickEvent event) {
204
//			    //				if(table.isEditable() == false){
205
//			    //					table.setEditable(true);
206
//			    for(Object prop:table.getContainerPropertyIds()){
207
//			            if(!prop.equals("Taxon")&&!prop.equals("Rank")){
208
//			                table.addGeneratedColumn(prop, createTableColumnGenerator());
209
//			            }
210
//			    }
211
////				}//else{
212
////					table.setEditable(false);
213
////					table.refreshRowCache();
214
////				}
215
//			}
216
//		});
217

  
218
		/**Double Click listener for Table*/
219
		table.addItemClickListener(new ItemClickListener() {
220
		    private static final long serialVersionUID = 1L;
221

  
222
		    @Override
223
		    public void itemClick(ItemClickEvent event) {
224
		        TaxonBase taxonBase = loadTaxonFromSelection(event);
225
		        currentTaxon = (Taxon)taxonBase;
226
		        if(event.isDoubleClick()){
227
		            if(!(event.getPropertyId().toString().equalsIgnoreCase("Taxon")) && !(event.getPropertyId().toString().equalsIgnoreCase("Rank"))){
228
		                if(!table.removeGeneratedColumn(event.getPropertyId())){
229
		                    table.addGeneratedColumn(event.getPropertyId(), createTableColumnGenerator());
230
		                    propertyList.add(event.getPropertyId());
231
		                }else{
232
		                    table.removeGeneratedColumn(event.getPropertyId());
233
		                    propertyList.remove(event.getPropertyId());
234
		                    redrawTable();
235
		                }
236
		            }
237
		        }
238
		    }
239
		});
240
	}
241

  
242
	private TaxonBase loadTaxonFromSelection(ItemClickEvent event) {
243
	    Item item = event.getItem();
244
	    Property itemProperty = item.getItemProperty("uuid");
245
	    UUID uuid = UUID.fromString(itemProperty.getValue().toString());
246
	    TaxonBase taxonBase = listener.getTaxonService().load(uuid);
247
	    return taxonBase;
248
	}
249

  
250
	private String refreshValue(ComboBox box, Object value){
251
	    if(box.getValue() == null){
252
	        if(value != null){
253
	            return value.toString();
254
	        }else{
255
	            return "click me for new Distribution Status";
256
	        }
257
	    }else{
258
	        return box.getValue().toString();
259
	    }
260
	}
261

  
262
	private ColumnGenerator createTableColumnGenerator(){
263

  
264
	    ColumnGenerator generator = new ColumnGenerator() {
265

  
266
	        private static final long serialVersionUID = 1L;
267

  
268
	        @Override
269
	        public Object generateCell(Table source, Object itemId, Object columnId) {
270
	            Property containerProperty = source.getContainerProperty(itemId, columnId);
271
	            Object value = null;
272
	            if(containerProperty != null){
273
	                value = containerProperty.getValue();
274
	            }
275
	            final UUID uuid = UUID.fromString(table.getItem(itemId).getItemProperty("uuid").getValue().toString());
276
	            final ComboBox box = new ComboBox("Occurrence Status: ", listener.getPresenceAbsenceContainer());
277
	            final String area = columnId.toString();
278
	            box.setImmediate(true);
279
	            final Object value1 = value;
280
	            box.addValueChangeListener(new ValueChangeListener() {
281
	                @Override
282
	                public void valueChange(ValueChangeEvent event) {
283
	                    Taxon taxon = (Taxon)listener.getTaxonService().load(uuid);
284

  
285
	                    if(value1==null){
286
	                        listener.createDistributionField(taxon, box.getValue(), area);
287
	                        Notification.show("Create Status", Notification.Type.TRAY_NOTIFICATION);
288
	                    }else{
289
	                        int result = listener.updateDistributionField(area, box.getValue(), taxon);
290
	                        if(result == 1){
291
	                            Notification.show("Delete Status", Notification.Type.TRAY_NOTIFICATION);
292
	                        }else if(result == 0){
293
	                            Notification.show("DescriptionService wrote", Notification.Type.TRAY_NOTIFICATION);
294
	                        }
295
	                    }
296
	                }
297
	            });
298
	            final PopupView popup = new PopupView(new PopupView.Content() {
299
	                private static final long serialVersionUID = 1L;
300
	                @Override
301
	                public String getMinimizedValueAsHTML() {
302
	                    return refreshValue(box, value1);
303
	                }
304
	                @Override
305
	                public Component getPopupComponent() {
306
	                    //FIXME: find a better solution
307
	                    box.setValue(compareObjectToPAT(value1));
308
	                    box.setBuffered(true);
309
	                    return box;
310
	                }
311
	            });
312
	            popup.addPopupVisibilityListener(new PopupVisibilityListener() {
313

  
314
	                @Override
315
	                public void popupVisibilityChange(PopupVisibilityEvent event) {
316

  
317
	                }
318
	            });
319
	            popup.setHideOnMouseOut(true);
320
	            return popup;
321
	        }
322
	    };
323
	    return generator;
324 209
	}
325 210

  
326 211
	private PresenceAbsenceTerm compareObjectToPAT(Object object){
......
333 218
	    return null;
334 219
	}
335 220

  
336
	private void redrawTable(){
337
	    try {
338
	        CdmSQLContainer sqlContainer = listener.getSQLContainer();
339
	        sqlContainer.refresh();
340
	        table.setContainerDataSource(sqlContainer);
341
	        table.setVisibleColumns(columnList.toArray());
342
	    } catch (SQLException e) {
343
	        e.printStackTrace();
344
	    }
345
	}
346

  
347

  
348 221
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/IDistributionTableComponent.java
37 37

  
38 38
		int getSizeOfTaxonNode();
39 39

  
40
		List<Distribution> getDistribution(Taxon taxon);
40
		List<Distribution> getDistributions(Taxon taxon);
41 41

  
42 42
		IClassificationService getClassificationService();
43 43

  

Also available in: Unified diff