package nc.vo.pub.filesystem;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.ValidationException;
import nc.vo.pub.ValueObject;


/**
 * 附件文件VO对象<br>
 * （V63新加描述，浏览次数，修改者等信息，并将此VO从private移到public。）<br>
 * @author modified by zhoujja 2012-10-17
 *
 */
public class NCFileVO extends ValueObject implements Serializable {
	private static final long serialVersionUID = -8538372121442731737L;
	private String pk = null;
	private List<String> segsList = new ArrayList<String>();
	private long time = System.currentTimeMillis();
	private long fileLen = 0;
	private String creator = null;
	private  boolean isFolder = false;
	
	/**是否引用文档*/
	private boolean isdoc =false ;
	/**引用文档的主键*/
	private String pk_doc;
	
	/**
	 * 附件描述 2012-9-30
	 */
	private String filedesc;
	/**
	 * 修改时间  2012-9-30
	 */
	private long modifytime ;
	/**
	 * 文件类型
	 */
	private String filetype ;
	
	public String getFiletype() {
		return filetype;
	}
	public void setFiletype(String filetype) {
		this.filetype = filetype;
	}
	public long getModifytime() {
		return modifytime;
	}
	public void setModifytime(long modifytime) {
		this.modifytime = modifytime;
	}
	public int getScantimes() {
		return scantimes;
	}
	public void setScantimes(int scantimes) {
		this.scantimes = scantimes;
	}
	public String getModifier() {
		return modifier;
	}
	public void setModifier(String modifier) {
		this.modifier = modifier;
	}
	/**
	 * 浏览次数 2012-9-30 
	 */
	private int scantimes;
	/**
	 * 修改人 2012-9-30
	 */
	private String modifier;
//	private String rootpath;
//	public String getRootpath() {
//		return rootpath;
//	}
//	public void setRootpath(String rootpath) {
//		this.rootpath = rootpath;
//	}
	public String getPk_doc() {
		return pk_doc;
	}
	public void setPk_doc(String pk_doc) {
		this.pk_doc = pk_doc;
	}
	public boolean isDoc() {
		return isdoc;
	}
	public void setIsdoc(boolean isdoc) {
		this.isdoc = isdoc;
	}
	public String getCreator() {
		return creator;
	}
	public void setCreator(String creator) {
		this.creator = creator;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}
	public long getFileLen() {
		return fileLen;
	}
	public void setFileLen(long fileLen) {
		this.fileLen = fileLen;
	}
	public String getName() {
		if(segsList.size() > 0)
			return segsList.get(segsList.size() - 1);
		else
			return null;
	}
	public void reName(String newName){
		if(newName.replace('\\', '/').indexOf('/') != -1){
			throw new RuntimeException(NCLangRes4VoTransl.getNCLangRes().getStrByID("sfbase", "NCFileVO-000000")/*文件名不能包含'\\'或者'/'*/);
		}
		if(segsList.size() > 0){
			 segsList.set(segsList.size() - 1, newName);
		}
	}
	public void setPath(String path) {
//		this.path = path;
		refreshSegsList(path);
	}
	private void refreshSegsList(String path){
		segsList.clear();
		if(path != null){
			StringTokenizer st = new StringTokenizer(path,"/");
			while (st.hasMoreTokens()) {
				String str = st.nextToken().trim();
				segsList.add(str);
			}
		}
	}
	public String getPk() {
		return pk;
	}
	public void setPk(String pk) {
		this.pk = pk;
	}
	public NCFileVO() {
		super();
	}
	public NCFileVO( String path) {
		super();
		setPath(path);
	}
	public String getParentpath() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < segsList.size()-1; i++) {
			if(sb.length() > 0)
				sb.append("/");
			sb.append(segsList.get(i));
		}
		return sb.toString();
	}
//	public void setParentpath(String parentpath) {
//		this.parentpath = parentpath;
//	}
	public String getFullPath(){
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < segsList.size(); i++) {
			if(sb.length() > 0)
				sb.append("/");
			sb.append(segsList.get(i));
		}
		return sb.toString();
	}
	public String getParentName(){
		String pName = null;
		int size = segsList.size();
		if(size >= 2){
			pName = segsList.get(size-2);
		}
		return pName;
	}
	public String getParentParentPath(){
		String ppPath = null;
		if(segsList.size() > 2){
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < segsList.size()-2; i++) {
				if(sb.length() > 0)
					sb.append("/");
				sb.append(segsList.get(i));
			}
			ppPath = sb.toString();
		}
		return ppPath;
	}
//	public List<String> getPathSegments(){
//		return segsList;
////		List<String> list = new ArrayList<String>();
////		if(getParentpath() != null && getParentpath().trim().length() > 0){
////			StringTokenizer st = new StringTokenizer(getParentpath(),"/");
////			while (st.hasMoreTokens()) {
////				String str = st.nextToken();
////				list.add(str);
////			}
////		}
////		list.add(getName());
////		return list;
//	}
	public boolean isFolder() {
		return isFolder;
	}
	public void setFolder(boolean isFolder) {
		this.isFolder = isFolder;
	}
	
	@Override
	public NCFileVO clone() {
		NCFileVO theVO =new NCFileVO();//(NCFileVO)super.clone();
		theVO.creator = this.creator;
		theVO.isFolder = this.isFolder;
		theVO.pk = this.pk;
		theVO.time = this.time;
		theVO.fileLen = this.fileLen;
		theVO.segsList = new ArrayList<String>();
		theVO.segsList.addAll(this.segsList);
		theVO.isdoc = this.isdoc;
		theVO.pk_doc=this.pk_doc;
		
		//新加字段
		theVO.modifier = this.modifier;
		theVO.filedesc = this.filedesc;
		theVO.modifytime = this.modifytime;
		theVO.scantimes=this.scantimes;		
		theVO.filetype=this.filetype;
		
		return theVO;
	}
	public void changeParent(NCFileVO parentFile,NCFileVO newParentFile){
		List<String> newList = new ArrayList<String>();
		newList.addAll(newParentFile.segsList);
		for (int i = parentFile.segsList.size(); i < segsList.size(); i++) {
			newList.add(segsList.get(i));
		}
		segsList.clear();
		segsList = newList;
	}
	public void setFiledesc(String filedesc) {
		this.filedesc = filedesc;
	}
	public String getFiledesc() {
		return filedesc;
	}
	@Override
	public String getEntityName() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void validate() throws ValidationException {
		// TODO Auto-generated method stub
		
	} 
}
