package org.yamcs.xtce;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yamcs.ConfigurationException;

/**
 * Abstract class for MDB loaders that load data from files (or directories with files)
 *  
 * @author nm
 *
 */
public abstract class AbstractFileLoader implements SpaceSystemLoader {
    protected final Logger log;
    protected String configName, path;
    
    
    public AbstractFileLoader(String path) throws ConfigurationException {
        this.path = path;
        this.configName=new File(path).getName()+"-"+path.hashCode();
        log=LoggerFactory.getLogger(getClass());
    }
    
    @Override
    public boolean needsUpdate(RandomAccessFile consistencyDateFile)  throws IOException, ConfigurationException {
        String line;
        while((line=consistencyDateFile.readLine())!=null) {
            if(line.startsWith(configName)) {
                File f=new File(path);
                if(!f.exists()) {
                    throw new ConfigurationException("The file "+path+" doesn't exist");
                }
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy/DDD HH:mm:ss");
                try {
                    Date serializedDate=sdf.parse(line.substring(configName.length()+1));
                    if(serializedDate.getTime()>=f.lastModified()) {
                        log.debug("Serialized {} is up to date", configName);
                        return false;
                    } else {
                        log.debug("Serialized {} is NOT up to date: serializedDate={}, mdbConsistencyDate={}", configName, serializedDate, new Date(f.lastModified()));
                        return true;
                    }
                } catch (ParseException e) {
                    log.warn("Cannot parse the date from "+line+": ", e);
                    return true;
                }
            }
        }
        log.info("Could not find a line starting with 'MDB' in the consistency date file");
        return true;
    }
    
    @Override
    public void writeConsistencyDate(FileWriter consistencyDateFile) throws IOException {
        File f=new File(path);
        consistencyDateFile.write(configName+" "+(new SimpleDateFormat("yyyy/DDD HH:mm:ss")).format(f.lastModified())+"\n");
    }


    @Override
    public String getConfigName() throws ConfigurationException {
        return configName;
    }   
}

@@ -413,17 +413,16 @@
 les)%0A * 
- 
 %0A * @aut
@@ -578,25 +578,16 @@
 , path;%0A
-    %0A    
 %0A    pub
@@ -703,17 +703,19 @@
 nfigName
-=
+ = 
 new File
@@ -734,13 +734,17 @@
 me()
-+%22-%22+
+ + %22-%22 + 
 path
@@ -767,17 +767,19 @@
      log
-=
+ = 
 LoggerFa
@@ -805,36 +805,32 @@
 Class());%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -896,17 +896,16 @@
 ateFile)
- 
  throws 
@@ -980,15 +980,18 @@
 hile
+ 
 ((line
-=
+ = 
 cons
@@ -1021,10 +1021,12 @@
 e())
+ 
 !=
+ 
 null
@@ -1043,16 +1043,17 @@
       if
+ 
 (line.st
@@ -1090,33 +1090,35 @@
           File f
-=
+ = 
 new File(path);%0A
@@ -1135,16 +1135,17 @@
       if
+ 
 (!f.exis
@@ -1220,14 +1220,18 @@
 le %22
-+
+ + 
 path
-+
+ + 
 %22 do
@@ -1299,17 +1299,19 @@
 rmat sdf
-=
+ = 
 new Simp
@@ -1406,17 +1406,19 @@
 izedDate
-=
+ = 
 sdf.pars
@@ -1453,17 +1453,19 @@
 length()
-+
+ + 
 1));%0A   
@@ -1483,16 +1483,17 @@
       if
+ 
 (seriali
@@ -1513,10 +1513,12 @@
 me()
-%3E=
+ != 
 f.la
@@ -1788,16 +1788,48 @@
 ate=%7B%7D%22,
+%0A                               
  configN
@@ -2044,14 +2044,18 @@
 om %22
-+
+ + 
 line
-+
+ + 
 %22: %22
@@ -2254,20 +2254,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Ov
@@ -2373,17 +2373,19 @@
   File f
-=
+ = 
 new File
@@ -2419,16 +2419,33 @@
 DateFile
+%0A                
 .write(c
@@ -2457,13 +2457,17 @@
 Name
-+%22 %22+
+ + %22 %22 + 
 (new
@@ -2530,17 +2530,19 @@
 ified())
-+
+ + 
 %22%5Cn%22);%0A 
@@ -2547,17 +2547,16 @@
 %0A    %7D%0A%0A
-%0A
     @Ove
@@ -2659,14 +2659,11 @@
 e;%0A    %7D
-   
 %0A%7D%0A

Use != instead of >= on date comparison check