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

📄 travelgroup.java

📁 旅游自助系统
💻 JAVA
字号:
package org.tshs.entity;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.tshs.core.CacheManager;
import org.tshs.core.Constant.ObjectType;
import org.tshs.storage.rdbms.ColumnInfo;
import org.tshs.storage.rdbms.DbManager;
import org.tshs.storage.rdbms.TableRow;

public class TravelGroup extends TshsObject{
	
	public static final int propertyLength = 5;
	
	private String name;
	
	/** Contains the descrition of duration time */
	private String description;
    
    private long capacity;
    
    private long total;

    private Date startTime;
    
    private Long corpId;
    
    private List<Long> clientIds;
    
    /** The order of the ids determine the sight spots sequence of the group's travel */
    private List<Long> sightIds;
    
    TravelGroup(Long id){
		super(id, ObjectType.GROUP);
    }
	
	/**
	 * @param row
	 */
	public TravelGroup(TableRow row) {
		super(Long.valueOf(row.getColumn("id").toString()), ObjectType.GROUP);
		setName((String)row.getColumn("name"));
		setDescription((String)row.getColumn("description"));
		setCapacity((Long)row.getColumn("capacity"));
		setTotal((Long)row.getColumn("total"));
		setStartTime(new Date(((java.sql.Date)row.getColumn("start")).getTime()));
		setCorpId(Long.valueOf(row.getColumn("corpId").toString()));
		List<Long> clientIds = (List<Long>) row.getColumn("clientIds");
		setClientIds(clientIds);
		List<Long> sightIds = (List<Long>) row.getColumn("sightIds");
		setSightIds(sightIds);
	}

	public void setName(String name){
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public void setDescription(String description){
		this.description = description;
	}
	
	public String getDescription(){
		return description;
	}
    
    public void setCapacity(long capacity){
    	this.capacity = capacity;
    }
    
    public long getCapacity(){
    	return capacity;
    }
    
    public void setTotal(long num){
    	this.total = num;
    }
    
    public long getTotal(){
    	return total;
    }
    
    public void setStartTime(Date startTime){
    	this.startTime = startTime;
    }
    
    public Date getStartTime(){
    	return startTime;
    }
    
    public void setCorpId(Long corpId){
    	this.corpId = corpId;
    }
    
    public Long getCorpId(){
    	return corpId;
    }
    
    public void setClientIds(List<Long> clientIds){
    	this.clientIds = clientIds;
    }
	
	public List<Long> getClientIds(){
		return clientIds;
	}
	
	public void setSightIds(List<Long> sightIds) {
		this.sightIds = sightIds;
	}
	
	public List<Long> getSightIds(){
		return sightIds;
	}
	
	public boolean checkPassword(String password){
		return true;
	}
    
    public List<Client> getAllClients() throws Exception{
    	List<Client> clientList = new ArrayList<Client>();
    	Iterator<Long> i = clientIds.iterator();
    	while(i.hasNext()){
    		clientList.add((Client)CacheManager.retrieve(i.next()));
    	}
    	return clientList;
    }
    
    public void setCorp(TravelCorp corp){
    	this.corpId = corp.getId();
    }
    
    public TravelCorp getCorp() throws Exception{
    	return (TravelCorp) CacheManager.retrieve(corpId);
    }
    
    /**
     * The second part of this method is in order to keep 
	 * the integrity of the relationship between clients and travelGroup.
     * 
     * @param client
     */
    public void addClient(Client client){
    	clientIds.add(client.getId());
    	
    	if(!client.isInGroup(this)){
    		client.addToGroup(this);
    	}
    }
    
    public void removeClient(Client client){
    	clientIds.remove(client.getId());
    	if(client.isInGroup(this)){
			client.removeGroup(this);
		}
    }
    
    public boolean hasClient(Client client){
		return clientIds.contains(client.getId());
	}
    
    /**
	 * The second part of this method is in order to keep 
	 * the integrity of the relationship between sightSpot and travelGroup.
     * 
     * @param sight
     */
    public void addSight(SightSpot sight){
    	sightIds.add(sight.getId());
    	
    	if(!sight.willRecieveGroup(this)){
    		sight.recieveGroup(this);
    	}
    }

	/**
	 * @param spot
	 * @return
	 */
	public boolean willVisitSight(SightSpot sight) {
		return sightIds.contains(sight);
	}

	public String toString(){
		return "ID:" + id + "\tE-mail: -" + "\tType:GROUP";
	}
	
	public Vector selfList() throws Exception{
		Vector<String> sightNames = new Vector<String>();
		Iterator i = sightIds.iterator();
		SightSpot sight = null;
		while (i.hasNext()) {
			sight = (SightSpot) CacheManager.getById((Long) i.next());
			sightNames.add(sight.getName());
		}
		
		Vector<Object> self = new Vector<Object>();
		self.add(name);
		self.add(description);
		self.add(capacity);
		self.add(total);
		self.add(startTime);
		self.add(getCorp().getName());
		self.add(sightNames);
		return self;
	}

	/**
	 * @param data
	 */
	public boolean modify(Vector data) {
		if(data.size() != propertyLength){
			System.out.println("Error from Client: data.size() != propertyLength");
			return false;
		}
		
		setName((String) data.get(0));
		setDescription((String)data.get(1));
		setCapacity((Integer)data.get(2));
		setTotal((Integer)data.get(3));
		setStartTime((Date)data.get(4));
		
		try {
			DbManager.update(buildTableRow());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return true;
	}
	
	protected TableRow buildTableRow(){
		List<String> names = new ArrayList<String>();
		List<Integer> types = new ArrayList<Integer>();
		
		names.add("id");
		types.add(Types.BIGINT);
		names.add("name");
		types.add(Types.VARCHAR);
		names.add("description");
		types.add(Types.VARCHAR);
		names.add("capacity");
		types.add(Types.INTEGER);
		names.add("total");
		types.add(Types.INTEGER);
		names.add("start");
		types.add(Types.DATE);
		
		ColumnInfo info = null;
		try {
			info = new ColumnInfo(names, types);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("name", name);
		map.put("description", description);
		map.put("capacity", capacity);
		map.put("total", total);
		map.put("start", startTime);
		TableRow row = new TableRow("travelgroup", map, info);
		
		return row;
	}
    
}

⌨️ 快捷键说明

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