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.ClassId;
8   import org.sourceforge.jemm.database.ClassInfo;
9   import org.sourceforge.jemm.database.StructureModifiedException;
10  import org.sourceforge.jemm.database.components.interfaces.DBClassHandler;
11  import org.sourceforge.jemm.database.components.se.StorageEngineClassIF;
12  
13  /**
14   * 
15   * @author Rory Graves
16   */
17  public class DefaultDBClassHandler implements DBClassHandler {
18      private final StorageEngineClassIF classIF;
19  
20      public DefaultDBClassHandler(StorageEngine storageEngine) {
21      	this.classIF = storageEngine.getClassIF();
22  	}
23  
24  	@Override
25  	public synchronized ClassInfo get(ClassId classId) {
26          if(classId == null)
27              throw new IllegalArgumentException("classId key can not be null");
28          
29          return classIF.getClassInfoById(classId);
30  	}
31  
32  	@Override
33  	public synchronized ClassId register(ClassInfo info) throws StructureModifiedException {
34          if(info == null)
35              throw new IllegalArgumentException("classInfo cannot be null");
36          
37          ClassId classId = classIF.getClassIdByName(info.className);
38          if(classId == null) {
39          	// not currently registered
40              classId = classIF.createClassId();
41              classIF.saveClassInfo(classId, info);
42              return classId;
43          } else {
44              ClassInfo storedInfo = classIF.getClassInfoById(classId);
45              if(!storedInfo.equals(info)) {
46              	if(allowModification(classId, storedInfo, info))
47              		classIF.saveClassInfo(classId,info);
48              	else
49              		throw new StructureModifiedException("Class modification on class " 
50              				+ info.className + " not  allowed");
51              }
52  
53              return classId;
54          }
55  	}
56  	
57  	/**
58  	 * Method to determine whether class modification is allowed.
59  	 * 
60  	 * Internal method to all sub-classes to extend default behaviour
61  	 * @param classId The classId The id of the registered class.
62  	 * @param current The current class structure
63  	 * @param proposed The proposed class structure
64  	 * @return Whether modification is allowed 
65  	 */
66  	protected boolean allowModification(ClassId classId,ClassInfo current,ClassInfo proposed) {
67  		return false;
68  	}
69  }