Project

General

Profile

Revision c92b53a5

IDc92b53a5fa8d5555e0c087596c9a30367485f97b
Parent eed6b243
Child e90e4ec3

Added by Fabian Reimeier over 2 years ago

ref #7039 made preparations to retrieve uneditable areas from preferences

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/view/distributionStatus/DistributionTablePresenter.java
87 87
        // no point subscribing
88 88
    }
89 89

  
90
    public int updateDistributionField(String distributionAreaString, Object comboValue, Taxon taxon) {
90
    public int updateDistributionField(NamedArea area, PresenceAbsenceTerm distributionStatus, Taxon taxon) {
91 91
	    TransactionStatus tx = repo.startTransaction();
92 92
	    taxon = (Taxon)repo.getTaxonService().find(taxon.getUuid());
93
	    Set<DefinedTermBase> chosenTerms = getChosenTerms();
94
	    NamedArea namedArea = null;
95
	    for(DefinedTermBase term:chosenTerms){
96
	    	Representation representation = term.getRepresentation(Language.DEFAULT());
97
	    	if(representation!=null){
98
	    		if(DistributionEditorUtil.isAbbreviatedLabels()){
99
	    			String label = representation.getLabel();
100
	    			String abbreviatedLabel = representation.getAbbreviatedLabel();
101
					if(abbreviatedLabel!=null && abbreviatedLabel.equalsIgnoreCase(distributionAreaString)){
102
	    				namedArea = (NamedArea) term;
103
	    				break;
104
	    			}
105
					else if(label!=null && label.equalsIgnoreCase(distributionAreaString)){
106
						namedArea = (NamedArea) term;
107
						break;
108
					}
109
	    		}
110
	    	}
111
	        if(term.getTitleCache().equalsIgnoreCase(distributionAreaString)){
112
	        	namedArea = (NamedArea) term;
113
	        	break;
114
	        }
115
	    }
116
	    if(namedArea==null){
93
	    if(area==null){
117 94
	    	Notification.show(Messages.getLocalizedString(Messages.DistributionTablePresenter_ERROR_UPDATE_DISTRIBUTION_TERM));
118 95
	    	repo.commitTransaction(tx);
119 96
	    	return -1;
......
121 98
	    List<Distribution> distributions = getDistributions(taxon);
122 99
	    Distribution distribution = null;
123 100
	    for(Distribution dist : distributions){
124
	        if(dist.getArea()!=null && dist.getArea().equals(namedArea)){
101
	        if(dist.getArea()!=null && dist.getArea().equals(area)){
125 102
	            distribution = dist;
126 103
	            break;
127 104
	        }
128 105
	    }
129 106
	    if(distribution==null){
130 107
	    	//create new distribution
131
	    	distribution = Distribution.NewInstance(namedArea, (PresenceAbsenceTerm) comboValue);
108
	    	distribution = Distribution.NewInstance(area, distributionStatus);
132 109
			Set<TaxonDescription> descriptions = taxon.getDescriptions();
133 110
			if (descriptions != null && !descriptions.isEmpty()) {
134 111
			    for (TaxonDescription desc : descriptions) {
......
144 121
			    return 0;
145 122
			}
146 123
	    }
147
	    else if(comboValue == null){//delete descriptionElementBase
124
	    else if(distributionStatus == null){//delete descriptionElementBase
148 125
	        DescriptionBase<?> desc = distribution.getInDescription();
149 126
	        desc.removeElement(distribution);
150 127
	    	repo.commitTransaction(tx);
151 128
            return 1;
152 129
	    }
153 130
	    else{//update distribution
154
           distribution.setStatus((PresenceAbsenceTerm)comboValue);
131
           distribution.setStatus(distributionStatus);
155 132
           repo.getCommonService().saveOrUpdate(distribution);
156 133
           repo.commitTransaction(tx);
157 134
           return 0;
......
160 137
	    return -1;
161 138
	}
162 139

  
163
    public LazyQueryContainer getAreaDistributionStatusContainer() {
164
         List<UUID> nodeUuids = getAllNodes().stream().map(n -> n.getUuid()).collect(Collectors.toCollection(ArrayList::new));
165
         List<NamedArea> namedAreas = getNamedAreas();
166
         if(namedAreas!=null){
167
             QueryFactory factory = new DistributionStatusQueryFactory(this.repo, nodeUuids, namedAreas);
168
             QueryDefinition defintion = new DistributionStatusQueryDefinition(namedAreas, true, 50);
169
             return new LazyQueryContainer(defintion, factory);
170
         }
171
         return null;
172
     }
173

  
174 140
    public CdmSQLContainer getSQLContainer() throws SQLException{
175 141
         List<Integer> nodeIds = new ArrayList<>();
176 142
         for (TaxonNode taxonNode : getAllNodes()) {
177 143
             nodeIds.add(taxonNode.getId());
178 144
         }
179
         List<NamedArea> namedAreas = getNamedAreas();
145
         List<NamedArea> namedAreas = getChosenAreas();
180 146
         if(namedAreas!=null){
181 147
             return new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namedAreas));
182 148
         }
......
193 159
        return listDescriptionElementsForTaxon;
194 160
    }
195 161

  
162
    public List<NamedArea> getUneditableAreas(){
163
        List<NamedArea> uneditable = new ArrayList<>();
164
        return uneditable;
165
    }
166

  
167
    public NamedArea getAreaFromString(String areaString){
168
        List<NamedArea> namedAreas = getChosenAreas();
169
        NamedArea area = null;
170
        for(NamedArea namedArea:namedAreas){
171
            Representation representation = namedArea.getRepresentation(Language.DEFAULT());
172
            if(representation!=null){
173
                if(DistributionEditorUtil.isAbbreviatedLabels()){
174
                    String label = representation.getLabel();
175
                    String abbreviatedLabel = representation.getAbbreviatedLabel();
176
                    if(abbreviatedLabel!=null && abbreviatedLabel.equalsIgnoreCase(areaString)){
177
                        area = namedArea;
178
                        break;
179
                    }
180
                    else if(label!=null && label.equalsIgnoreCase(areaString)){
181
                        area = namedArea;
182
                        break;
183
                    }
184
                }
185
            }
186
            if(namedArea.getTitleCache().equalsIgnoreCase(areaString)){
187
                area = namedArea;
188
                break;
189
            }
190
        }
191
        return area;
192
    }
193

  
196 194
    private List<Distribution> getDistributions(Taxon taxon) {
197 195
        Set<Feature> setFeature = new HashSet<>(Arrays.asList(Feature.DISTRIBUTION()));
198 196
        List<Distribution> listTaxonDescription = CdmSpringContextHelper.getDescriptionService()
......
201 199

  
202 200
    }
203 201

  
204
    private Set<DefinedTermBase> getChosenTerms() {
205
        VaadinSession session = VaadinSession.getCurrent();
206
        UUID vocUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREA_VOCABULARY_UUID);
207
//      getConversationHolder().getSession();
208
        TermVocabulary<DefinedTermBase> voc = CdmSpringContextHelper.getVocabularyService().load(vocUUID, Arrays.asList("terms")); //$NON-NLS-1$
209
//      voc = CdmBase.deproxy(voc);
210
        return voc.getTerms();
211
    }
212

  
213
	private List<NamedArea> getNamedAreas(){
202
	private List<NamedArea> getChosenAreas(){
214 203
	    List<NamedArea> namedAreas = (List<NamedArea>)VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREAS);
215 204
	    if(namedAreas!=null && namedAreas.isEmpty()){
216
	        return getNamedAreasFromVoc();
205
	        return getChosenAreasFromVoc();
217 206
	    }
218 207
	    return namedAreas;
219 208
	}
220 209

  
221
	private List<NamedArea> getNamedAreasFromVoc(){
210
	private List<NamedArea> getChosenAreasFromVoc(){
222 211
	    VaadinSession session = VaadinSession.getCurrent();
223 212
	    UUID vocUUID = (UUID) session.getAttribute(DistributionEditorUtil.SATTR_SELECTED_AREA_VOCABULARY_UUID);
224 213
	    TermVocabulary<NamedArea> vocabulary = CdmSpringContextHelper.getVocabularyService().load(vocUUID, Arrays.asList("terms")); //$NON-NLS-1$
......
233 222

  
234 223
	}
235 224

  
225
   private List<TaxonNode> getChosenTaxonNodes() {
226
        VaadinSession session = VaadinSession.getCurrent();
227
        List<UUID> taxonNodeUUIDs = (List<UUID>) session.getAttribute(DistributionEditorUtil.SATTR_TAXON_NODES_UUID);
228
        UUID classificationUuid = (UUID)session.getAttribute(DistributionEditorUtil.SATTR_CLASSIFICATION);
229
        if((taxonNodeUUIDs==null || taxonNodeUUIDs.isEmpty()) && classificationUuid!=null){
230
            Classification classification = CdmSpringContextHelper.getClassificationService().load(classificationUuid);
231
            if(classification!=null){
232
                taxonNodeUUIDs = Collections.singletonList(classification.getRootNode().getUuid());
233
            }
234
        }
235
        List<TaxonNode> loadedNodes = CdmSpringContextHelper.getTaxonNodeService().load(taxonNodeUUIDs, null);
236
        if(loadedNodes!=null){
237
            return loadedNodes;
238
        }
239
        return Collections.emptyList();
240
    }
241

  
236 242
	private List<TaxonNode> getAllNodes(){
237 243
		List<TaxonNode> allNodes = new ArrayList<>();
238 244

  
......
246 252
		return allNodes;
247 253
	}
248 254

  
249
	private List<TaxonNode> getChosenTaxonNodes() {
250
		VaadinSession session = VaadinSession.getCurrent();
251
		List<UUID> taxonNodeUUIDs = (List<UUID>) session.getAttribute(DistributionEditorUtil.SATTR_TAXON_NODES_UUID);
252
		UUID classificationUuid = (UUID)session.getAttribute(DistributionEditorUtil.SATTR_CLASSIFICATION);
253
		if((taxonNodeUUIDs==null || taxonNodeUUIDs.isEmpty()) && classificationUuid!=null){
254
			Classification classification = CdmSpringContextHelper.getClassificationService().load(classificationUuid);
255
			if(classification!=null){
256
				taxonNodeUUIDs = Collections.singletonList(classification.getRootNode().getUuid());
257
			}
258
		}
259
		List<TaxonNode> loadedNodes = CdmSpringContextHelper.getTaxonNodeService().load(taxonNodeUUIDs, null);
260
		if(loadedNodes!=null){
261
			return loadedNodes;
262
		}
263
		return Collections.emptyList();
264
	}
265

  
266 255
	protected static final List<String> DESCRIPTION_INIT_STRATEGY = Arrays.asList(new String []{
267 256
            "$", //$NON-NLS-1$
268 257
            "elements.*", //$NON-NLS-1$
......
328 317
	/**Unused Methods*/
329 318
	// TODO: Currently unused. Remove?
330 319
    private List<String> getAbbreviatedNamedAreas() {
331
        List<NamedArea> terms = getNamedAreasFromVoc();
320
        List<NamedArea> terms = getChosenAreasFromVoc();
332 321
        List<String> list = new ArrayList<>();
333 322
        for(DefinedTermBase<?> dtb: terms){
334 323
            for(Representation r : dtb.getRepresentations()){
......
356 345
        return map;
357 346
    }
358 347

  
359

  
348
    public LazyQueryContainer getAreaDistributionStatusContainer() {
349
        List<UUID> nodeUuids = getAllNodes().stream().map(n -> n.getUuid()).collect(Collectors.toCollection(ArrayList::new));
350
        List<NamedArea> namedAreas = getChosenAreas();
351
        if(namedAreas!=null){
352
            QueryFactory factory = new DistributionStatusQueryFactory(this.repo, nodeUuids, namedAreas);
353
            QueryDefinition defintion = new DistributionStatusQueryDefinition(namedAreas, true, 50);
354
            return new LazyQueryContainer(defintion, factory);
355
        }
356
        return null;
357
    }
360 358
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/distributionStatus/DistributionTableViewBean.java
43 43
import eu.etaxonomy.cdm.model.common.Representation;
44 44
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
45 45
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
46
import eu.etaxonomy.cdm.model.location.NamedArea;
46 47
import eu.etaxonomy.cdm.model.taxon.Taxon;
47 48
import eu.etaxonomy.cdm.vaadin.component.distributionStatus.AreaAndTaxonSettingsConfigWindow;
48 49
import eu.etaxonomy.cdm.vaadin.component.distributionStatus.DetailWindow;
......
154 155
            		// TODO: HACK FOR RL 2017, REMOVE AS SOON AS POSSIBLE
155 156
            		&& !(event.getPropertyId().toString().equalsIgnoreCase("DE"))
156 157
            		&& !(event.getPropertyId().toString().equalsIgnoreCase("Deutschland"))){
157
                final Item item = event.getItem();
158
                Property<?> itemProperty = item.getItemProperty("uuid");
159
                UUID uuid = UUID.fromString(itemProperty.getValue().toString());
160
                final Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService()
161
                		.load(uuid,Arrays.asList("descriptions.descriptionElements","name.taxonBases","updatedBy")), Taxon.class);
162
                final String areaID = (String)event.getPropertyId();
163
                PresenceAbsenceTerm presenceAbsenceTerm = null;
164
                Object statusValue = item.getItemProperty(areaID).getValue();
165
                if(statusValue instanceof String){
166
                    try {
167
                        presenceAbsenceTerm = (PresenceAbsenceTerm)CdmSpringContextHelper.getTermService().load(UUID.fromString((String)statusValue));
168
                    }catch(IllegalArgumentException|ClassCastException e) {
169
                        // Not a PresenceAbsenceTerm Column
158

  
159
                final String areaString = (String)event.getPropertyId();
160
                final NamedArea area = getPresenter().getAreaFromString(areaString);
161

  
162
                if(!getPresenter().getUneditableAreas().contains(area)) {
163
                    final Item item = event.getItem();
164
                    Property<?> itemProperty = item.getItemProperty("uuid");
165
                    UUID uuid = UUID.fromString(itemProperty.getValue().toString());
166
                    final Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService()
167
                    		.load(uuid,Arrays.asList("descriptions.descriptionElements","name.taxonBases","updatedBy")), Taxon.class);
168
                    PresenceAbsenceTerm presenceAbsenceTerm = null;
169
                    Object statusValue = item.getItemProperty(areaString).getValue();
170
                    if(statusValue instanceof String){
171
                        try {
172
                            presenceAbsenceTerm = (PresenceAbsenceTerm)CdmSpringContextHelper.getTermService().load(UUID.fromString((String)statusValue));
173
                        }catch(IllegalArgumentException|ClassCastException e) {
174
                            // Not a PresenceAbsenceTerm Column
175
                        }
170 176
                    }
177
                    //popup window
178
                    final Window popup = new Window(Messages.getLocalizedString(Messages.DistributionTableViewBean_CHOOSE_DISTRIBUTION_STATUS));
179
                    DelegatingErrorHandler errorHandler = new DelegatingErrorHandler();
180
                    errorHandler.registerHandler(new HibernateExceptionHandler());
181
                    popup.setErrorHandler(errorHandler);
182
                    final ListSelect termSelect = new ListSelect();
183
                    termSelect.setSizeFull();
184
                    termSelect.setContainerDataSource(getPresenter().getPresenceAbsenceTermContainer());
185
                    termSelect.setNullSelectionAllowed(presenceAbsenceTerm != null);
186
                    if(presenceAbsenceTerm != null){
187
                    	termSelect.setNullSelectionItemId(Messages.getLocalizedString(Messages.DistributionTableViewBean_NO_STATUS_SELECT));
188
                    }else{
189
                        logger.debug("No distribution status exists yet for area");
190
                    }
191
                    termSelect.setValue(presenceAbsenceTerm);
192
                    termSelect.addValueChangeListener(valueChangeEvent -> {
193
    						PresenceAbsenceTerm distributionStatus = (PresenceAbsenceTerm) valueChangeEvent.getProperty().getValue();
194
    						getPresenter().updateDistributionField(area, distributionStatus, taxon);
195
    						container.refresh();
196
    						popup.close();
197
    				});
198
                    VerticalLayout layout = new VerticalLayout(termSelect);
199
                    popup.setContent(layout);
200
                    popup.setModal(true);
201
                    popup.center();
202
                    UI.getCurrent().addWindow(popup);
171 203
                }
172
                //popup window
173
                final Window popup = new Window(Messages.getLocalizedString(Messages.DistributionTableViewBean_CHOOSE_DISTRIBUTION_STATUS));
174
                DelegatingErrorHandler errorHandler = new DelegatingErrorHandler();
175
                errorHandler.registerHandler(new HibernateExceptionHandler());
176
                popup.setErrorHandler(errorHandler);
177
                final ListSelect termSelect = new ListSelect();
178
                termSelect.setSizeFull();
179
                termSelect.setContainerDataSource(getPresenter().getPresenceAbsenceTermContainer());
180
                termSelect.setNullSelectionAllowed(presenceAbsenceTerm != null);
181
                if(presenceAbsenceTerm != null){
182
                	termSelect.setNullSelectionItemId(Messages.getLocalizedString(Messages.DistributionTableViewBean_NO_STATUS_SELECT));
183
                }else{
184
                    logger.debug("No distribution status exists yet for area");
185
                }
186
                termSelect.setValue(presenceAbsenceTerm);
187
                termSelect.addValueChangeListener(valueChangeEvent -> {
188
						Object distributionStatus = valueChangeEvent.getProperty().getValue();
189
						getPresenter().updateDistributionField(areaID, distributionStatus, taxon);
190
						container.refresh();
191
						popup.close();
192
				});
193
                VerticalLayout layout = new VerticalLayout(termSelect);
194
                popup.setContent(layout);
195
                popup.setModal(true);
196
                popup.center();
197
                UI.getCurrent().addWindow(popup);
198 204
            }
199 205
        });
200 206

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)