⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 raplasql.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    }

    public void loadAll() throws RaplaException, SQLException {
    	categoriesWithoutParent.clear();
    	super.loadAll();
    	// then we rebuild the hirarchy
    	Iterator it = categoriesWithoutParent.entrySet().iterator();
    	while (it.hasNext()) {
    		Map.Entry entry = (Map.Entry)it.next();
    		Object parentId = entry.getValue();
    		Category category = (Category) entry.getKey();
    		Category parent;
            Assert.notNull( category );
    		if ( parentId != null) {
    		    parent = (Category) resolve( parentId );
                Assert.notNull( parent );
            } else {
    		    parent = getSuperCategory();
                Assert.notNull( parent );
            }
    		
            parent.addCategory( category );
    	}
    }
}

class AllocatableStorage extends RaplaTypeStorage {
    Map classificationMap = new HashMap();
    Map allocatableMap = new HashMap();
    AttributeValueStorage resourceAttributeStorage;
    PermissionStorage permissionStorage;

    public AllocatableStorage(RaplaContext context, boolean oldResourceTable) throws RaplaException {
        super(context,Allocatable.TYPE, oldResourceTable ? "RESOURCE" : "RAPLA_RESOURCE",new String [] {"ID","TYPE_KEY","IS_PERSON","IGNORE_CONFLICTS"});
        resourceAttributeStorage = new AttributeValueStorage(context,"RESOURCE_ATTRIBUTE_VALUE", "RESOURCE_ID",classificationMap);
        permissionStorage = new PermissionStorage( context, allocatableMap);
        addSubStorage(resourceAttributeStorage);
        addSubStorage(permissionStorage );
    }

    protected void write(PreparedStatement stmt,RefEntity entity) throws SQLException,RaplaException {
      	AllocatableImpl resource = (AllocatableImpl) entity;
        if ( getLogger().isDebugEnabled())
      	    getLogger().debug("Inserting Resource " + resource.getName(getLocale()));
      	int id = getId( entity );
      	String typeKey = resource.getClassification().getType().getElementKey();
		stmt.setInt(1, id );
      	stmt.setString(2, typeKey );
      	stmt.setInt(3, resource.isPerson()? 1:0);
      	stmt.setInt(4, resource.isHoldBackConflicts()? 1:0);
      	stmt.executeUpdate();
    }

    protected void load(ResultSet rset) throws SQLException {
        int idInt = rset.getInt(1);
    	String typeKey = rset.getString(2);
    	boolean isPerson = rset.getInt( 3 ) == 1;
    	boolean ignoreConflicts = rset.getInt( 4 ) == 1;

    	AllocatableImpl allocatable = new AllocatableImpl();
    	allocatable.setPerson( isPerson );
    	allocatable.setId( new SimpleIdentifier( allocatable.getRaplaType() , idInt));
    	allocatable.setHoldBackConflicts( ignoreConflicts );
    	DynamicType type = getDynamicType(typeKey );
        if ( type == null)
        {
            getLogger().error("Allocatable with id " + idInt + " has an unknown type " + typeKey + ". Try ignoring it");
            return;
        }
        
    	Classification classification = type.newClassification();
    	allocatable.setClassification( classification );
    	classificationMap.put( new Integer(idInt), classification );
    	allocatableMap.put(  new Integer(idInt), allocatable);
    	put( allocatable );
    }

   

    public void loadAll() throws RaplaException, SQLException {
    	classificationMap.clear();
    	super.loadAll();
    }
}

class ReservationStorage extends RaplaTypeStorage {
    Map classificationMap = new HashMap();
    Map reservationMap = new HashMap();
    AttributeValueStorage attributeValueStorage;
    public ReservationStorage(RaplaContext context) throws RaplaException {
        super(context,Reservation.TYPE, "EVENT",new String [] {"ID","TYPE_KEY","OWNER_ID","CREATION_TIME","LAST_CHANGED","LAST_CHANGED_BY"});
        attributeValueStorage = new AttributeValueStorage(context,"EVENT_ATTRIBUTE_VALUE","EVENT_ID", classificationMap);
        addSubStorage(attributeValueStorage);
    }

    protected void write(PreparedStatement stmt,RefEntity entity) throws SQLException,RaplaException {
      	Reservation event = (Reservation) entity;
      	if ( getLogger().isDebugEnabled())
      	    getLogger().debug("Storing Reservation " + event.getName(getLocale()));
      	int id = getId( entity );
      	String typeKey = event.getClassification().getType().getElementKey();
      	int userId = getId( (RefEntity) event.getOwner() );
      	stmt.setInt(1, id );
      	stmt.setString(2, typeKey );
    	stmt.setInt(3, userId );
    	Date creationTime = event.getCreateTime();
    	Date lastModified = event.getLastChangeTime();
    	stmt.setTimestamp( 4, new java.sql.Timestamp( creationTime.getTime() ));
    	stmt.setTimestamp( 5, new java.sql.Timestamp( lastModified.getTime() ));
        User lastChangedBy = event.getLastChangedBy();
    	if ( lastChangedBy != null) {
            int lastChangedById = getId( (RefEntity) lastChangedBy );
            stmt.setInt( 6, lastChangedById);
        } else {
            stmt.setObject(6, null);
        }
        stmt.executeUpdate();
    }

    protected void load(ResultSet rset) throws SQLException {
        int idInt = rset.getInt(1);
    	String typeKey = rset.getString(2);
    	int userInt = rset.getInt(3);
    	java.sql.Timestamp creationTime = rset.getTimestamp( 4 );
    	java.sql.Timestamp lastModified = rset.getTimestamp( 5 );
    	ReservationImpl event = new ReservationImpl(new Date( creationTime.getTime()), new Date( lastModified.getTime()));
    	event.setId( new SimpleIdentifier(Reservation.TYPE, idInt));
    	DynamicType type = getDynamicType(typeKey );
        User user = (User)get( new SimpleIdentifier(User.TYPE, userInt));
        if ( user == null || type == null)
        {
            getLogger().warn("Reservation with id " + idInt + " has no type or owner. It will be ignored");
            return;
        }
    	event.setOwner( user );
        int lastModfiedByIdInt = rset.getInt( 6);
        if ( !rset.wasNull()) {
            User lastModifiedBy = (User)get( new SimpleIdentifier(User.TYPE, lastModfiedByIdInt));
            if ( lastModifiedBy != null)
            {
                event.setLastChangedBy( lastModifiedBy );
            }
        }

    	Classification classification = type.newClassification();
    	event.setClassification( classification );
    	classificationMap.put( new Integer(idInt), classification );
    	reservationMap.put( new Integer(idInt), event );
    	put( event );
    }

    public void loadAll() throws RaplaException, SQLException {
    	classificationMap.clear();
    	super.loadAll();
    }
}

/** This class should only be used whitin the ResourceStorage class*/
class AttributeValueStorage extends EntityStorage {
    Map classificationMap;
    final String foreignKeyName;
    public AttributeValueStorage(RaplaContext context,String tablename, String foreignKeyName, Map classificationMap) throws RaplaException {
        super( context, tablename,new String [] {foreignKeyName,"ATTRIBUTE_KEY","VALUE"});
        this.foreignKeyName = foreignKeyName;
        this.classificationMap = classificationMap;
    }

    protected void write(PreparedStatement stmt,RefEntity classifiable) throws EntityNotFoundException, SQLException {
        int id = getId(classifiable);
        Classification classification = ((Classifiable) classifiable).getClassification();;
        Attribute[] attributes = classification.getAttributes();
        for (int i=0;i<attributes.length;i++) {
            Attribute attribute = attributes[i];
            Object value = classification.getValue( attribute );
            if ( value == null) {
                continue;
            }
            
            String valueAsString = AttributeImpl.attributeValueToString( attribute, value, true);
            if ( valueAsString == null)
            {
                continue;
            }
            
            stmt.setInt(1, id);
            stmt.setString(2, attribute.getKey());
         	stmt.setString(3, valueAsString);
         	stmt.execute();
        }
    }

    public void save( RefEntity entity ) throws RaplaException, SQLException{
        delete( entity );
        insert( entity );
    }

    protected void load(ResultSet rset) throws SQLException, RaplaException {
        int classifiableIdInt = rset.getInt( 1);
        String attributekey = rset.getString( 2 );
        Classification classification = (Classification) classificationMap.get(new Integer(classifiableIdInt));
        if ( classification == null) {
            getLogger().warn("No resource or reservation found for the id" + classifiableIdInt  + " ignoring.");
            return;
        }
    	Attribute attribute = classification.getType().getAttribute( attributekey );
    	if ( attribute == null) {
    		throw new EntityNotFoundException("DynamicType '" +classification.getType() +"' doesnt have an attribute with the key " + attributekey + " Current Allocatable Id " + classifiableIdInt);
    	}
    	String valueAsString = rset.getString( 3);
	    Object value = null;
    	try {
    	    if ( valueAsString != null)
            {
    	        value = AttributeImpl.parseAttributeValue(attribute,valueAsString, getResolver()) ;
                classification.setValue( attributekey, value);
            }
    	} catch (ParseException ex) {
    	    throw new RaplaException( "Error parsing attribute value: " +ex.getMessage(), ex );
    	}
    }

 

    public void delete( RefEntity entity) throws SQLException {
        int classifiableId = getId( entity );
        executeBatchedStatement(con, "DELETE FROM " + tableName + " WHERE " + foreignKeyName + " = " + classifiableId);
    }
}

class PermissionStorage extends EntityStorage  {
    Map allocatableMap;
    public PermissionStorage(RaplaContext context,Map allocatableMap) throws RaplaException {
        super(context,"PERMISSION",new String[] {"RESOURCE_ID","USER_ID","GROUP_ID","ACCESS_LEVEL","MIN_ADVANCE","MAX_ADVANCE","START_DATE","END_DATE"});
        this.allocatableMap = allocatableMap;
    }

    protected void write(PreparedStatement stmt, RefEntity allocatable) throws SQLException, RaplaException {
        int resourceId = getId(  allocatable);
        delete( allocatable );
        Permission[] permissions = ((Allocatable)allocatable).getPermissions();
        for (int i=0;i<permissions.length;i++) {
            Permission s = permissions[i];
		    Category group = s.getGroup();
			User user = s.getUser();
			Date start = s.getStart();
			Date end = s.getEnd();
			Long minAdvance = s.getMinAdvance();
			Long maxAdvance = s.getMaxAdvance();
			stmt.setInt(1, resourceId);
			if ( user != null ) {
			    int userId = getId( (RefEntity) user );
			    stmt.setInt( 2, userId );
			} else {
			    stmt.setObject( 2, null);
			}
			if ( group != null) {
			    int groupId = getId( (RefEntity) group);
			    stmt.setInt( 3, groupId );
			} else {
			    stmt.setObject( 3, null);
			}
			int accessLevel = s.getAccessLevel();
			stmt.setInt(4, accessLevel );
			if ( minAdvance != null) {
			    stmt.setInt( 5, minAdvance.intValue());
			} else {
			    stmt.setObject(5, null);
			}
			if ( maxAdvance != null) {
			    stmt.setInt( 6, maxAdvance.intValue());
			} else {
			    stmt.setObject(6, null);
			}
			if ( start != null) {
			    stmt.setTimestamp( 7, new java.sql.Timestamp( start.getTime() ));
			} else {
			    stmt.setObject(7, null);
			}
			if ( end != null) {
			    stmt.setTimestamp( 8, new java.sql.Timestamp( end.getTime() ));
			} else {
			    stmt.setObject(8, null);
			}
			stmt.executeUpdate();
		}
    }

    public void save( RefEntity entity ) throws RaplaException, SQLException{
        delete( entity );
        insert( entity );
    }

    protected void load(ResultSet rset) throws SQLException, RaplaException {
        int allocatableIdInt = rset.getInt( 1);
        Allocatable allocatable = (Allocatable) allocatableMap.get(new Integer(allocatableIdInt));
        PermissionImpl permission = new PermissionImpl();
        allocatable.addPermission( permission );
        int userIdInt = rset.getInt( 2);
        if ( !rset.wasNull()) {
            permission.getReferenceHandler().putId("user", new SimpleIdentifier( User.TYPE,userIdInt));
        }
        int groupIdInt = rset.getInt( 3);
        if ( !rset.wasNull()) {
            permission.getReferenceHandler().putId("group", new SimpleIdentifier( Category.TYPE,groupIdInt));
        }
        int accessLevel = rset.getInt( 4);
        permission.setAccessLevel( accessLevel );
        int minAdvance = rset.getInt( 5 );
        if ( !rset.wasNull()) {
            permission.setMinAdvance( new Long( minAdvance ));
        }
        int maxAdvance = rset.getInt( 6 );
        if ( !rset.wasNull()) {
            permission.setMaxAdvance( new Long( maxAdvance ));
        }
        Timestamp startDate = rset.getTimestamp( 7 );
        if ( !rset.wasNull()) {
            permission.setStart( new Date(startDate.getTime()));
        }
        Timestamp endDate = rset.getTimestamp( 8 );
        if ( !rset.wasNull()) {
            permission.setEnd( new Date(endDate.getTime()));
        }
    }

    public void delete( RefEntity entity) throws SQLException {
        int resourceId = getId( entity ) ;
        executeBatchedStatement(con, "DELETE FROM " + tableName + " WHERE RESOURCE_ID = " + resourceId);
    }

}


class AppointmentStorage extends RaplaTypeStorage {
    AppointmentExceptionStorage appointmentExceptionStorage;
    AllocationStorage allocationStorage;
    public AppointmentStorage(RaplaContext context) throws RaplaException {
        super(context, Appointment.TYPE,"APPOINTMENT",new String [] {"ID","EVENT_ID","APPOINTMENT_START","APPOINTMENT_END","REPETITION_TYPE","REPETITION_NUMBER", "REPETITION_END", "REPETITION_INTERVAL"});
        appointmentExceptionStorage = new AppointmentExceptionStorage(context);
        allocationStorage = new AllocationStorage( context);
        addSubStorage(appointmentExceptionStorage);
        addSubStorage(allocationStorage);
    }

    protected void write(PreparedStatement stmt,RefEntity entity) throws SQLException,RaplaException {
        Appointment appointment = (Appointment) entity;
      	int id = getId( entity );
      	Timestamp start = new Timestamp( appointment.getStart().getTime() );
      	Timestamp end = new Timestamp( appointment.getEnd().getTime() );
      	stmt.setInt(1, id );
      	int parentId = getId( (RefEntity)appointment.getReservation() );
      	stmt.setInt(2, parentId );
      	stmt.setTimestamp(3, start );
      	stmt.setTimestamp(4, end );
      	Repeating repeating = appointment.getRepeating();
      	if ( repeating == null) {
      	    stmt.setObject(5, null);
      	    stmt.setObject(6, null);
      	    stmt.setObject(7, null);
      	    stmt.setObject(8, null);
      	} else {
      	    stmt.setString(5, repeating.getType().toString());
      	    int number = repeating.getNumber();
      	    if ( number >= 0) {
      	        stmt.setInt(6, number);
      	    }  else {
      	        stmt.setObject(6, null);
      	    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -