package roart.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Date;

import roart.dao.IndexFilesDao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class IndexFiles {

	private static Log log = LogFactory.getLog("IndexFiles");
	private String md5;
	private Boolean indexed;
    private String timeclass;
	private String timeindex;
	private String timestamp;
	private String convertsw;
	private String converttime;
    private String classification;
	private Integer failed;
    private String failedreason;
    private String noindexreason;
    private String timeoutreason;
	private Set<FileLocation> filelocations;

    private boolean changed = false;
    private boolean indb = false;

	private IndexFiles() {
	    filelocations = new HashSet<FileLocation>();
	}

	public IndexFiles(String md5) {
	    filelocations = new HashSet<FileLocation>();
	    this.setMd5(md5);
	}

        public String getMd5() {
	    return md5;
	}

	public void setMd5(String md5) {
	    this.md5 = md5;
	}

        public String getFilename() {
	    if (filelocations.size() == 0) {
		return null;
	    }
	    return ((FileLocation) (getFilelocations()).iterator().next()).getFilename();
	}

        public String getFilelocation() {
	    if (filelocations.size() == 0) {
		return null;
	    }
	    return ((FileLocation) (getFilelocations()).iterator().next()).toString();
	}

        public Boolean getIndexed() {
	    return indexed;
	}

	public void setIndexed(Boolean indexed) {
	    changed |= true;
	    this.indexed = indexed;
	}

        public Date getTimestampDate() {
	    if (timestamp == null) {
		return new Date(0);
	    }
	    return new Date(timestamp);
	}

        public String getTimestamp() {
	    return timestamp;
	}

	public void setTimestamp(String timestamp) {
	    changed |= true;
	    this.timestamp = timestamp;
	}

        public String getTimeindex() {
	    return timeindex;
	}

        public String getTimeindex(String format) {
	    if (timeindex == null) {
		return null;
	    }
	    int fl = timeindex.indexOf(".");
	    if (fl >= 0) {
		timeindex = timeindex.substring(0, fl-1) + "000"; // temp
	    }
	    return String.format(format, (float) new Long(timeindex).longValue()/1000);
	}

	public void setTimeindex(String timeindex) {
	    changed |= true;
	    this.timeindex = timeindex;
	}

	public void setTimeindex(long millis) {
	    changed |= true;
	    this.timeindex = "" + millis;
	}

        public String getConvertsw() {
	    return convertsw;
	}

	public void setConvertsw(String convertsw) {
	    changed |= true;
	    this.convertsw = convertsw;
	}

        public String getConverttime() {
	    return converttime;
	}

        public String getConverttime(String format) {
	    if (converttime == null) {
		return null;
	    }
	    int fl = converttime.indexOf(".");
	    if (fl >= 0) {
		converttime = converttime.substring(0, fl-1) + "000"; // temp
	    }
	    return String.format(format, (float) new Long(converttime).longValue()/1000);
	}

	public void setConverttime(String converttime) {
	    changed |= true;
	    this.converttime = converttime;
	}

	public void setConverttime(long millis) {
	    changed |= true;
	    this.converttime = "" + millis;
	}

	public String getClassification() {
	    return classification;
	}

	public void setClassification(String classification) {
	    changed |= true;
	    this.classification = classification;
	}

	public void setTimeclass(String timeclass) {
	    changed |= true;
	    this.timeclass = timeclass;
	}

	public void setTimeclass(long millis) {
	    changed |= true;
	    this.timeclass = "" + millis;
	}

        public String getTimeclass() {
	    return timeclass;
	}

        public String getTimeclass(String format) {
	    if (timeclass == null) {
		return null;
	    }
	    int fl = timeclass.indexOf(".");
	    if (fl >= 0) {
		timeclass = timeclass.substring(0, fl-1) + "000"; // temp
	    }
	    return String.format(format, (float) new Long(timeclass).longValue()/1000);
	}

        public Integer getFailed() {
	    if (failed == null) {
		failed = new Integer(0);
	    }
	    return failed;
	}

	public void setFailed(Integer failed) {
	    changed |= true;
	    this.failed = failed;
	}

        public void incrFailed() {
	    changed |= true;
	    if (failed == null) {
		failed = new Integer(0);
	    }
	    failed++;
	}

        public Set<FileLocation> getFilelocations() {
	    return filelocations;
	}

        public Set<String> getFilenames() {
	    Set<String> names = new HashSet<String>();
	    for (FileLocation fl : filelocations) {
		names.add(fl.getFilename());
	    }
	    return names;
	}

	public void setFilelocations(Set<FileLocation> filelocations) {
	    changed |= true;
	    this.filelocations = filelocations;
	}

	public void addFile(FileLocation filelocation) {
	    changed |= true;
	    filelocations.add(filelocation);
	    //IndexFilesDao.ensureExistence(filelocation);
	}

	public void addFile(String filename) {
	    String nodename = roart.util.Prop.getProp().getProperty("nodename");
	    FileLocation fl = new FileLocation(nodename, filename);
	    addFile(fl);
	}

	public boolean removeFile(String filename) {
	    String nodename = roart.util.Prop.getProp().getProperty("nodename");
	    FileLocation fl = new FileLocation(nodename, filename);
	    return filelocations.remove(fl);
	}

	public boolean removeFilelocation(FileLocation fl) {
	    return filelocations.remove(fl);
	}

        public String getTimeoutreason() {
	    return timeoutreason;
	}

	public void setTimeoutreason(String timeoutreason) {
	    changed |= true;
	    this.timeoutreason = timeoutreason;
	}

        public String getNoindexreason() {
	    return noindexreason;
	}

	public void setNoindexreason(String noindexreason) {
	    changed |= true;
	    this.noindexreason = noindexreason;
	}

        public String getFailedreason() {
	    return failedreason;
	}

	public void setFailedreason(String failedreason) {
	    changed |= true;
	    this.failedreason = failedreason;
	}

    /*
    public void save() {
	all.remove(this);
	IndexFilesDao.save(this);
    }
    */

    public boolean hasChanged() {
	return changed;
    }

    public void setUnchanged() {
	changed = false;
    }

    public void setDbNot() {
	indb = true;
    }

    public boolean inDbNot() {
	return indb;
    }

    }

@@ -1822,32 +1822,41 @@
 0);%0A%09    %7D%0A%09    
+try %7B %0A%09%09
 return new Date(
@@ -1867,16 +1867,147 @@
 stamp);%0A
+%09    %7D catch (Exception e) %7B%0A%09%09log.error(%22Exception from %22 + timestamp);%0A%09%09log.error(%22Exception%22, e);%0A%09%09return new Date(0);%0A%09    %7D%0A
 %09%7D%0A%0A    

handle date exception