View Javadoc

1   /*
2    * Created on 27 Mar 2009
3    *
4    */
5   package org.sourceforge.jemm.database.components;
6   
7   import org.sourceforge.jemm.database.EnumId;
8   import org.sourceforge.jemm.database.EnumInfo;
9   import org.sourceforge.jemm.database.StructureModifiedException;
10  import org.sourceforge.jemm.database.components.interfaces.DBEnumHandler;
11  import org.sourceforge.jemm.database.components.se.StorageEngineEnumIF;
12  
13  
14  /**
15   * @author Rory Graves
16   *
17   */
18  public class DefaultDBEnumHandler implements DBEnumHandler {
19      private final StorageEngineEnumIF enumIF;
20  
21      public DefaultDBEnumHandler(StorageEngine storageEngine) {
22      	this.enumIF = storageEngine.getEnumIF();
23  	}
24  
25  	@Override
26  	public synchronized EnumInfo get(EnumId enumId) {
27          if(enumId == null)
28              throw new IllegalArgumentException("enumId key can not be null");
29          
30          return enumIF.getEnumInfoById(enumId);
31  	}
32  
33  	@Override
34  	public synchronized EnumId register(EnumInfo info) throws StructureModifiedException {
35          if(info == null)
36              throw new IllegalArgumentException("EnumInfo cannot be null");
37          
38          EnumId enumId = enumIF.getEnumIdByName(info.getClassName());
39          if(enumId == null) {
40          	// not currently registered
41              enumId = enumIF.createEnumId();
42              enumIF.saveEnumInfo(enumId, info);
43              return enumId;
44          } else {
45              EnumInfo storedInfo = enumIF.getEnumInfoById(enumId);
46              if(!storedInfo.equals(info)) {
47              	if(allowModification(enumId, storedInfo, info))
48              		enumIF.saveEnumInfo(enumId,info);
49              	else
50              		throw new StructureModifiedException("Class modification not allowed");
51              }
52              return enumId;
53          }
54  	}
55  	
56  	/**
57  	 * Check method to determine whether an enum can be modified.
58  	 * Internal method to all sub-classes to extend default behaviour
59  	 * @param enumId The identity of the enumeration.
60  	 * @param current The current enumeration values.
61  	 * @param proposed The proposed enumeration values.
62  	 * @return True if the enum may be modified false otherwise.
63  	 */
64  	protected boolean allowModification(EnumId enumId,EnumInfo current,EnumInfo proposed) {
65  		return false;
66  	}
67  }