Project

General

Profile

« Previous | Next » 

Revision ede17590

Added by Andreas Müller about 13 years ago

made propertry change events configurable (maybe solving #2426)

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/facade/DerivedUnitFacade.java
169 169
	}
170 170

  
171 171
	private final DerivedUnitFacadeConfigurator config;
172
	
173
	private Map<PropertyChangeListener, CdmBase> listeners = new HashMap<PropertyChangeListener, CdmBase>();
172 174

  
173 175
	// private GatheringEvent gatheringEvent;
174 176
	private DerivedUnitType type; // needed?
......
192 194
	 * @return
193 195
	 */
194 196
	public static DerivedUnitFacade NewInstance(DerivedUnitType type) {
195
		return new DerivedUnitFacade(type, null);
197
		return new DerivedUnitFacade(type, null, null);
196 198
	}
197 199
	
198 200
	/**
......
203 205
	 * @return
204 206
	 */
205 207
	public static DerivedUnitFacade NewInstance(DerivedUnitType type, FieldObservation fieldObservation) {
206
		return new DerivedUnitFacade(type, fieldObservation);
208
		return new DerivedUnitFacade(type, fieldObservation, null);
209
	}
210

  
211
	/**
212
	 * Creates a derived unit facade for a new derived unit of type
213
	 * <code>type</code>.
214
	 * 
215
	 * @param type
216
	 * @param fieldObservation the field observation to use
217
	 * @param config the facade configurator to use
218
	 * //TODO are there any ambiguities to solve with defining a field observation or a configurator 
219
	 * @return
220
	 */
221
	public static DerivedUnitFacade NewInstance(DerivedUnitType type, FieldObservation fieldObservation, DerivedUnitFacadeConfigurator config) {
222
		return new DerivedUnitFacade(type, fieldObservation, config);
207 223
	}
208 224

  
225
	
209 226
	/**
210 227
	 * Creates a derived unit facade for a given derived unit using the default
211 228
	 * configuration.
......
227 244

  
228 245
	// ****************** CONSTRUCTOR ******************************************
229 246

  
230
	private DerivedUnitFacade(DerivedUnitType type, FieldObservation fieldObservation) {
231
		this.config = DerivedUnitFacadeConfigurator.NewInstance();
247
	private DerivedUnitFacade(DerivedUnitType type, FieldObservation fieldObservation, DerivedUnitFacadeConfigurator config) {
248
		if (config == null){
249
			config = DerivedUnitFacadeConfigurator.NewInstance();
250
		}
251
		this.config = config;
232 252
		this.type = type;
233 253
		// derivedUnit
234 254
		derivedUnit = type.getNewDerivedUnitInstance();
......
268 288
				fieldObservation = fieldOriginals.iterator().next();
269 289
				// ###fieldObservation =
270 290
				// getInitializedFieldObservation(fieldObservation);
271
				fieldObservation
272
						.addPropertyChangeListener(getNewEventPropagationListener());
291
				if (config.isFirePropertyChangeEvents()){
292
					addNewEventPropagationListener(fieldObservation);
293
				}
273 294
			} else {
274 295
				throw new IllegalStateException("Illegal state");
275 296
			}
......
1721 1742
	private void setFieldObservation(FieldObservation fieldObservation) {
1722 1743
		this.fieldObservation = fieldObservation;
1723 1744
		if (fieldObservation != null){
1724
			fieldObservation.addPropertyChangeListener(getNewEventPropagationListener());
1745
			if (config.isFirePropertyChangeEvents()){
1746
				addNewEventPropagationListener(fieldObservation);
1747
			}
1725 1748
			if (derivedUnit != null){
1726 1749
				DerivationEvent derivationEvent = getDerivationEvent(CREATE);
1727 1750
				derivationEvent.addOriginal(fieldObservation);
......
1762 1785

  
1763 1786
	// Determination
1764 1787
	public void addDetermination(DeterminationEvent determination) {
1765
		testDerivedUnit(); 
1788
		testDerivedUnit();
1766 1789
		determination.setIdentifiedUnit(derivedUnit);
1767 1790
		derivedUnit.addDetermination(determination);
1768 1791
	}
......
2210 2233
	}
2211 2234

  
2212 2235
	// ******************************* Events ***************************
2213

  
2236
	
2214 2237
	/**
2215 2238
	 * @return
2216 2239
	 */
2217
	private PropertyChangeListener getNewEventPropagationListener() {
2240
	private void addNewEventPropagationListener(CdmBase listeningObject) {
2241
		//if there is already a listener, don't do anything
2242
		for (PropertyChangeListener listener : this.listeners.keySet()){
2243
			if (listeners.get(listener) == listeningObject){
2244
				return;
2245
			}
2246
		}
2247
		//create new listener
2218 2248
		PropertyChangeListener listener = new PropertyChangeListener() {
2219 2249
			@Override
2220 2250
			public void propertyChange(PropertyChangeEvent event) {
......
2227 2257
				}
2228 2258
			}
2229 2259
		};
2230
		return listener;
2260
		//add listener to listening object and to list of listeners
2261
		listeningObject.addPropertyChangeListener(listener);
2262
		listeners.put(listener, listeningObject);
2231 2263
	}
2232 2264

  
2233
	// **************** Other Collections
2234
	// ***************************************************
2265
	// **************** Other Collections ********************************
2235 2266

  
2236 2267
	/**
2237 2268
	 * Creates a duplicate specimen which derives from the same derivation event
......
2306 2337
		return type;
2307 2338
	}
2308 2339

  
2340
	
2341
	/**
2342
	 * Closes this facade. As a minimum this method removes all listeners created by this facade from their 
2343
	 * listening objects.
2344
	 */
2345
	public void close(){
2346
		for (PropertyChangeListener listener : this.listeners.keySet()){
2347
			CdmBase listeningObject = listeners.get(listener);
2348
			listeningObject.removePropertyChangeListener(listener);
2349
		}
2350
	}
2309 2351
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/facade/DerivedUnitFacadeCacheStrategy.java
64 64
		
65 65
		DerivedUnitFacade facade;
66 66
		try {
67
			facade = DerivedUnitFacade.NewInstance(derivedUnit);
67
			DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
68
			config.setFirePropertyChangeEvents(false);
69
			facade = DerivedUnitFacade.NewInstance(derivedUnit, config);
68 70
			result += fieldStrategy.getFieldData(facade);
69 71
//			//country
70 72
//			String strCountry = null;
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/facade/DerivedUnitFacadeConfigurator.java
34 34
	private boolean moveDerivedUnitMediaToGallery = false;
35 35
	
36 36
	private boolean throwExceptionForNonSpecimenPreservationMethodRequest = true;
37
	
38
	private boolean firePropertyChangeEvents = true;
37 39

  
38 40
	
39 41
	//needed if inititialization via property paths is required
......
109 111
	public boolean isThrowExceptionForNonSpecimenPreservationMethodRequest() {
110 112
		return throwExceptionForNonSpecimenPreservationMethodRequest;
111 113
	}
114

  
115
	public void setFirePropertyChangeEvents(boolean firePropertyChangeEvents) {
116
		this.firePropertyChangeEvents = firePropertyChangeEvents;
117
	}
118

  
119
	public boolean isFirePropertyChangeEvents() {
120
		return firePropertyChangeEvents;
121
	}
112 122
	
113 123
	
114 124
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/facade/DerivedUnitFacadeFieldObservationCacheStrategy.java
60 60
	public String getTitleCache(FieldObservation fieldObservation) {
61 61
		DerivedUnitFacade facade;
62 62
		String result = "";
63
		facade = DerivedUnitFacade.NewInstance(DerivedUnitType.FieldObservation, fieldObservation);
63
		DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
64
		config.setFirePropertyChangeEvents(false);
65
		facade = DerivedUnitFacade.NewInstance(DerivedUnitType.FieldObservation, fieldObservation, config);
64 66
		result = getFieldData(facade);	
65 67
		result = addPlantDescription(result, facade);
66
		
68
		facade.close();
67 69
		return result;
68 70
	}
69 71
	
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/facade/DerivedUnitFacadeTest.java
22 22
import org.junit.BeforeClass;
23 23
import org.junit.Ignore;
24 24
import org.junit.Test;
25
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
26
import org.springframework.security.core.context.SecurityContextHolder;
25 27
import org.unitils.dbunit.annotation.DataSet;
26 28
import org.unitils.dbunit.annotation.ExpectedDataSet;
27 29
import org.unitils.spring.annotation.SpringBeanByType;
......
29 31
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
30 32
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
31 33
import eu.etaxonomy.cdm.api.service.ITermService;
34
import eu.etaxonomy.cdm.api.service.IUserService;
32 35
import eu.etaxonomy.cdm.model.agent.AgentBase;
33 36
import eu.etaxonomy.cdm.model.agent.Team;
34 37
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
35 38
import eu.etaxonomy.cdm.model.common.Language;
36 39
import eu.etaxonomy.cdm.model.common.LanguageString;
37 40
import eu.etaxonomy.cdm.model.common.TimePeriod;
41
import eu.etaxonomy.cdm.model.common.User;
38 42
import eu.etaxonomy.cdm.model.description.Feature;
39 43
import eu.etaxonomy.cdm.model.description.Sex;
40 44
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
......
66 70
 * 
67 71
 */
68 72
public class DerivedUnitFacadeTest extends CdmTransactionalIntegrationTest {
69
	private static final Logger logger = Logger
70
			.getLogger(DerivedUnitFacadeTest.class);
73
	private static final Logger logger = Logger.getLogger(DerivedUnitFacadeTest.class);
71 74

  
72 75
	@SpringBeanByType
73 76
	private IOccurrenceService service;
......
1592 1595
		// this should not throw exceptions
1593 1596
		specimenFacade.setAbsoluteElevationRange(minimum, maximum);
1594 1597
	}
1598
	
1599
	@SpringBeanByType
1600
	private IUserService userService;
1601
	
1602
	/**
1603
	 * 
1604
	 * See https://dev.e-taxonomy.eu/trac/ticket/2426
1605
	 * This test doesn't handle the above issue yet as it doesn't fire events as 
1606
	 * expected (at least it does not reproduce the behaviour in the Taxonomic Editor).
1607
	 * In the meanwhile the property change framework for the facade has been changed
1608
	 * so the original problem may have disappeared.
1609
	 * 
1610
	 */
1611
	@Test
1612
	public void testNoRecursiveChangeEvents(){
1613
		String username = "username";
1614
		String password = "password";
1615
		User user = User.NewInstance(username, password);
1616
		userService.save(user);
1617
		UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, password);
1618
		SecurityContextHolder.getContext().setAuthentication(token);
1619
		
1620
		DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
1621
		facade.setLocality("testLocality");
1622
		facade.getTitleCache();
1623
//		facade.innerGatheringEvent().firePropertyChange("createdBy", null, user);
1624
		this.service.save(facade.innerDerivedUnit());
1625
		
1626
	}
1627
	
1595 1628
}

Also available in: Unified diff