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

📄 raplasql.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      	    Date repeatingEnd = repeating.getEnd();
      	    if ( repeatingEnd != null) {
      	        stmt.setObject(7, new Timestamp( repeatingEnd.getTime()));
      	    } else
      	    {
      	        stmt.setObject(7, null);
      	    }
      	    int interval = repeating.getInterval();
      	    stmt.setInt(8, interval);
      	}
      	stmt.executeUpdate();
    }

    protected void load(ResultSet rset) throws SQLException, EntityNotFoundException {
        int idInt = rset.getInt(1);
        int parentId = rset.getInt( 2 );
        Reservation event;
        try {
            event = (Reservation) resolve( new SimpleIdentifier( Reservation.TYPE, parentId));
        } 
        catch ( EntityNotFoundException ex)
        {
            getLogger().warn("Could not find reservation object with id "+ parentId + " for appointment with id " + idInt );
            return;
        }
        Date start = new Date(rset.getTimestamp(3).getTime());
        Date end = new Date(rset.getTimestamp(4).getTime());
    	AppointmentImpl appointment = new AppointmentImpl(start, end);
    	appointment.setId( new SimpleIdentifier(Appointment.TYPE, idInt));
    	event.addAppointment( appointment );
    	String repeatingType = rset.getString( 5 );
    	if ( !rset.wasNull() ) {
    	    appointment.setRepeatingEnabled( true );
    	    Repeating repeating = appointment.getRepeating();
    	    repeating.setType( RepeatingType.findForString( repeatingType ) );
    	    int number  = rset.getInt( 6);
    	    if ( !rset.wasNull()) {
    	        repeating.setNumber( number);
    	    } else {
    	        java.sql.Timestamp repeatingEnd = rset.getTimestamp( 7 );
    	        if ( !rset.wasNull() ) {
    	            repeating.setEnd( new Date(repeatingEnd.getTime()));
    	        } else {
                    repeating.setEnd( null );
                }
    	    }

    	    int interval = rset.getInt( 8);
    	    if ( !rset.wasNull())
    	        repeating.setInterval( interval);
    	}
    	put( appointment );
    }
}


class AllocationStorage extends EntityStorage  {

    public AllocationStorage(RaplaContext context) throws RaplaException  {
        super(context,"ALLOCATION",new String [] {"APPOINTMENT_ID", "RESOURCE_ID"});
    }

    protected void write(PreparedStatement stmt, RefEntity entity) throws SQLException, RaplaException {
        int appointmentId = getId( entity);
        Appointment appointment = (Appointment) entity;
        Reservation event = appointment.getReservation();
        Allocatable[] allocatables = event.getAllocatables();
        for (int j=0;j<allocatables.length;j++) {
            Allocatable allocatable  =  allocatables[j];
            if ( !event.hasAllocated( allocatable, appointment)) {
                continue;
            }
            int allocatableId = getId( (RefEntity)allocatable);
    		stmt.setInt(1, appointmentId);
            stmt.setInt(2, allocatableId);
    		stmt.executeUpdate();
        }
    }
    public void save( RefEntity entity ) throws RaplaException, SQLException{
        delete( entity );
        insert( entity );
    }

    protected void load(ResultSet rset) throws SQLException, RaplaException {
        int appointmentId = rset.getInt( 1 );
        int resourceId = rset.getInt( 2 );
        Appointment appointment;
        try {
            appointment = (Appointment)resolve( new SimpleIdentifier( Appointment.TYPE,appointmentId));
        }
        catch ( EntityNotFoundException ex)
        {
            getLogger().warn("Could not find appointment with id "+ appointmentId + " for the allocation resource " + resourceId + ". Ignoring." );
            return;
        }
        Reservation event = appointment.getReservation();
        Allocatable allocatable = (Allocatable)resolve( new SimpleIdentifier( Allocatable.TYPE,resourceId));
        if ( !event.hasAllocated( allocatable ) ) {
            event.addAllocatable( allocatable );
        }
        Appointment[] appointments = event.getRestriction( allocatable );
        Appointment[] newAppointments = new Appointment[ appointments.length+ 1];
        System.arraycopy(appointments,0, newAppointments, 0, appointments.length );
        newAppointments[ appointments.length] = appointment;
        if (event.getAppointments().length > newAppointments.length ) {
            event.setRestriction( allocatable, newAppointments );
        } else {
            event.setRestriction( allocatable, new Appointment[] {} );
        }
    }

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


 }

class AppointmentExceptionStorage extends EntityStorage  {
    public AppointmentExceptionStorage(RaplaContext context) throws RaplaException {
        super(context,"APPOINTMENT_EXCEPTION",new String [] {"APPOINTMENT_ID","EXCEPTION_DATE"});
    }


    protected void write(PreparedStatement stmt, RefEntity entity) throws SQLException, RaplaException {
        int appointmentId = getId( entity);
        Appointment appointment = (Appointment) entity;
        Repeating repeating = appointment.getRepeating();
        if ( repeating == null) {
            return;
        }
        Date[] exceptions = repeating.getExceptions();
	    for ( int i=0;i< exceptions.length;i++) {
	        java.sql.Timestamp exception = new java.sql.Timestamp( exceptions[i].getTime());
	        stmt.setInt( 1, appointmentId );
	        stmt.setTimestamp( 2, exception );
	        stmt.executeUpdate();
	    }
	}

    protected void load(ResultSet rset) throws SQLException, RaplaException {
        int appointmentId = rset.getInt( 1);
        Appointment appointment;
        try {
            appointment = (Appointment)resolve( new SimpleIdentifier( Appointment.TYPE,appointmentId));
        }
        catch ( EntityNotFoundException ex)
        {
            getLogger().warn("Could not find appointment with id "+ appointmentId + " for the specified exception. Ignoring." );
            return;
        }
                
        Repeating repeating = appointment.getRepeating();
        if ( repeating != null) {
            Date date = new Date( rset.getDate( 2 ).getTime());
            repeating.addException( date );
        }
    }

    

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

}

class DynamicTypeStorage extends RaplaTypeStorage {

    public DynamicTypeStorage(RaplaContext context) throws RaplaException {
        super(context, DynamicType.TYPE,"DYNAMIC_TYPE",
                new String [] {"ID","TYPE_KEY","DEFINITION"});
    }

	protected void write(PreparedStatement stmt,RefEntity entity) throws SQLException, RaplaException {
        stmt.setInt(1,getId(entity));
        DynamicType type = (DynamicType) entity;
        stmt.setString(2, type.getElementKey());
        stmt.setString(3,  getXML( type) );
        stmt.executeUpdate();
    }

	protected void load(ResultSet rset) throws SQLException,RaplaException {
    	String xml = getString(rset,3);
    	processXML( DynamicType.TYPE, xml );
	}

}


class PreferenceStorage extends RaplaTypeStorage {

    public PreferenceStorage(RaplaContext context) throws RaplaException {
        super(context,Preferences.TYPE,"PREFERENCE",
	    new String [] {"USER_ID","ROLE","STRING_VALUE","XML_VALUE"});
    }

    protected void write(PreparedStatement stmt, RefEntity entity) throws SQLException, RaplaException {
        PreferencesImpl preferences = (PreferencesImpl) entity;
        User user = (User) preferences.getOwner();
        if ( user == null) {
            stmt.setObject( 1, null);
        } else {
            stmt.setInt(1,getId( (RefEntity) user));
        }
        Iterator it = preferences.getPreferenceEntries();
        while (it.hasNext()) {
            String role = (String) it.next();
            Object entry = preferences.getEntry(role);
            stmt.setString( 2, role);
            if ( entry instanceof String) {
            	stmt.setString( 3, (String) entry);
            	stmt.setString( 4, null);
            } else {
            	//System.out.println("Role " + role + " CHILDREN " + conf.getChildren().length);
            	String xml = getXML( (RaplaObject)entry);
            	stmt.setString( 3, null);
            	stmt.setString( 4, xml);
            }
            stmt.executeUpdate();
        }
    }

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

    protected void load(ResultSet rset) throws SQLException, RaplaException {
    	//findPreferences
    	//check if value set
    	//  yes read value
    	//  no read xml

        int userIdAsInt = rset.getInt(1);
        User owner = null;
        Object preferenceId;
        if ( !rset.wasNull() ){
            Object userId = new SimpleIdentifier( User.TYPE, userIdAsInt );
            owner = (User) get( userId );
            preferenceId = new SimpleIdentifier( Preferences.TYPE, userIdAsInt );
        } else {
        	preferenceId = new SimpleIdentifier( Preferences.TYPE, 0 );
        }
        PreferencesImpl preferences = (PreferencesImpl) get( preferenceId );
        if ( preferences == null) {
        	preferences = new PreferencesImpl();
        	preferences.setId(preferenceId);
        	preferences.setOwner(owner);
        	put( preferences );
        }
        String configRole = getString( rset, 2);
        String value = rset.getString( 3 );
        if ( !rset.wasNull()) {
            preferences.putEntry(configRole, value);
        } else {
	        String xml = rset.getString( 4 );

	        PreferenceReader contentHandler = (PreferenceReader) processXML( Preferences.TYPE, xml );
	        try {
	            RaplaObject type = contentHandler.getChildType();
	            preferences.putEntry(configRole, type);
	        } catch (SAXException ex) {
	            throw new RaplaException (ex);
	        }
        }
    }

 

    public void delete( RefEntity entity) throws SQLException {
        PreferencesImpl preferences = (PreferencesImpl) entity;
        User user = (User) preferences.getOwner();
        if ( user != null) {
        	int userId = getId( (RefEntity) user ) ;
        	executeBatchedStatement(con, "DELETE FROM " + tableName + " WHERE USER_ID = " + userId);
        } else {
        	executeBatchedStatement(con, "DELETE FROM " + tableName + " WHERE USER_ID = null");
        }
    }

 }

class UserStorage extends RaplaTypeStorage {
    UserGroupStorage groupStorage;
    public UserStorage(RaplaContext context) throws RaplaException {
        super( context,User.TYPE, "RAPLA_USER",
	    new String [] {"ID","USERNAME","PASSWORD","NAME","EMAIL","ISADMIN"});
        groupStorage = new UserGroupStorage( context );
        addSubStorage( groupStorage );
    }

    protected void write(PreparedStatement stmt,RefEntity entity) throws SQLException, RaplaException {
        User user = (User) entity;
        if ( getLogger().isDebugEnabled())
            getLogger().debug("Inserting User " + user.getUsername());
        stmt.setInt(1,getId(entity));
       stmt.setString(2,user.getUsername());
       String password = cache.getPassword(entity.getId());
       stmt.setString(3,password);
       stmt.setString(4,user.getName());
       stmt.setString(5,user.getEmail());
       stmt.setInt(6,user.isAdmin()?1:0);
       stmt.executeUpdate();
    }

    protected void load(ResultSet rset) throws SQLException, RaplaException {
        int idAsInt = rset.getInt(1);
        String username = getString(rset,2);
        String name = getString(rset,4);
        String email = getString(rset,5);
        boolean isAdmin = rset.getInt( 6) == 1;
        UserImpl user = new UserImpl();
        Object userId = new SimpleIdentifier(User.TYPE, idAsInt );
        user.setId( userId );
        user.setUsername( username );
        user.setName( name );
        user.setEmail( email );
        user.setAdmin( isAdmin );
        String password = getString(rset,3);
        if ( !rset.wasNull()) {
            putPassword(userId,password);
        }
        put(user);
   }

   
}

class UserGroupStorage extends EntityStorage {
    public UserGroupStorage(RaplaContext context) throws RaplaException {
        super(context,"RAPLA_USER_GROUP", new String [] {"USER_ID","CATEGORY_ID"});
    }

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

    protected void write(PreparedStatement stmt, RefEntity entity) throws SQLException, RaplaException {
        int userId = getId( entity);
        User user = (User) entity;
        stmt.setInt(1, userId);
        Category[] categories = user.getGroups();
        for (int i=0;i<categories.length;i++) {
            stmt.setInt( 2, getId( (RefEntity)categories[i]));
            stmt.executeUpdate();
	    }
    }

    protected void load(ResultSet rset) throws SQLException, RaplaException {
        int userId = rset.getInt( 1);
        int categoryId = rset.getInt( 2);
        User user = (User)resolve( new SimpleIdentifier( User.TYPE,userId));
        Category category = (Category)resolve( new SimpleIdentifier( Category.TYPE,categoryId));
        user.addGroup( category);
    }

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


⌨️ 快捷键说明

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