// Path: src/main/java/no/met/metadataeditor/EditorException.java
// public class EditorException extends RuntimeException {
//     
//     // available error codes
//     public final static int GENERAL_ERROR_CODE = 100; // exception that cannot be futher classified    
//     public final static int TEMPLATE_PARSE_ERROR = 101; // an error when parsing the template
//     public final static int IO_ERROR = 102; // error caused by and IOException
//     public final static int METADATA_PARSE_ERROR = 103; // error caused when parsing the metadata content
//     public final static int EDITOR_CONFIG_UNMARSHAL_ERROR = 104; // error when converting editor config to objects
//     public final static int MISSING_PROJECT_CONFIG = 105; // project that user ask for has not been configured
//     public final static int MISSING_TEMPLATE = 106; // the template for a format is missing
//     public final static int MISSING_EDITOR_CONFIG = 107; //the editor configuration for a format is missing
//     public final static int UNSUPPORTED_FORMAT = 108; // the format for the content is not supported.
//     public final static int MISSING_METADATA_RESOURCE = 109; // the metadata resources is missing.
//     public final static int SETUP_XML_ERROR = 110; // error in the setup.xml file
//     
//     /**
//      * The unique error code within the exception type for this type of exception. 
//      */
//     public final int errorCode;
//     
//     /**
//      *
//      */
//     private static final long serialVersionUID = -7731932552297906249L;
// 
//     public EditorException(String message, int errorCode){
//         super(message);
//         this.errorCode = errorCode;
//     }
// 
//     public EditorException(String message, Throwable cause, int errorCode) {
//         super(message,cause);
//         this.errorCode = errorCode;        
//     }
// 
//     public EditorException(Throwable e, int errorCode) {
//         super(e);
//         this.errorCode = errorCode;        
//     }
//     
//     // needed for use of error code in the JSF.
//     public int getErrorCode(){
//         return errorCode;
//     }
// }
// 
// Path: src/main/java/no/met/metadataeditor/datastore/MetadataRecords.java
// @XmlAccessorType(XmlAccessType.NONE)
// public static class ResourceMetadata {
// 
//     private String name;
//     private String relativeUrl;
//     private Date lastModified;
// 
//     public ResourceMetadata() {
//     }
// 
//     public ResourceMetadata(String name, String relativeUrl, Date lastMdified) {
//         this.name = name;
//         this.relativeUrl = relativeUrl;
//         this.lastModified = lastMdified;
//     }
// 
//     @XmlAttribute
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     @XmlAttribute
//     public String getRelativeUrl() {
//         return relativeUrl;
//     }
// 
//     public void setRelativeUrl(String relativeUrl) {
//         this.relativeUrl = relativeUrl;
//     }
// 
//     @XmlAttribute
//     @XmlJavaTypeAdapter(DateAdapter.class)
//     public Date getLastModified() {
//         return lastModified;
//     }
// 
//     public void setLastModified(Date lastModified) {
//         this.lastModified = lastModified;
//     }
// }


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import no.met.metadataeditor.EditorException;
import no.met.metadataeditor.datastore.MetadataRecords.ResourceMetadata;
import org.apache.commons.io.FileUtils;

    @Override
    public boolean userHasWriteAccess(String username, String password) {

        // For the disk data store we assume that all user have access to write.
        return true;
    }

    @Override
    public String getDefaultUser(){
        return "";
    }

    @Override
    public String getDefaultPassword(){
        return "";
    }

    @Override
    public boolean delete(String url, String username, String password){
        return FileUtils.deleteQuietly(new File(url));
    }

    @Override
    public List<MetadataRecords.ResourceMetadata> listMetadataRecord() {
        //only allow .xml files
        Collection<File> files = FileUtils.listFiles(new File(metadataDirUrl()), new String[]{"xml"}, false);

        List<ResourceMetadata> records = new ArrayList<>();
        for(File f : files){
// Path: src/main/java/no/met/metadataeditor/EditorException.java
// public class EditorException extends RuntimeException {
//     
//     // available error codes
//     public final static int GENERAL_ERROR_CODE = 100; // exception that cannot be futher classified    
//     public final static int TEMPLATE_PARSE_ERROR = 101; // an error when parsing the template
//     public final static int IO_ERROR = 102; // error caused by and IOException
//     public final static int METADATA_PARSE_ERROR = 103; // error caused when parsing the metadata content
//     public final static int EDITOR_CONFIG_UNMARSHAL_ERROR = 104; // error when converting editor config to objects
//     public final static int MISSING_PROJECT_CONFIG = 105; // project that user ask for has not been configured
//     public final static int MISSING_TEMPLATE = 106; // the template for a format is missing
//     public final static int MISSING_EDITOR_CONFIG = 107; //the editor configuration for a format is missing
//     public final static int UNSUPPORTED_FORMAT = 108; // the format for the content is not supported.
//     public final static int MISSING_METADATA_RESOURCE = 109; // the metadata resources is missing.
//     public final static int SETUP_XML_ERROR = 110; // error in the setup.xml file
//     
//     /**
//      * The unique error code within the exception type for this type of exception. 
//      */
//     public final int errorCode;
//     
//     /**
//      *
//      */
//     private static final long serialVersionUID = -7731932552297906249L;
// 
//     public EditorException(String message, int errorCode){
//         super(message);
//         this.errorCode = errorCode;
//     }
// 
//     public EditorException(String message, Throwable cause, int errorCode) {
//         super(message,cause);
//         this.errorCode = errorCode;        
//     }
// 
//     public EditorException(Throwable e, int errorCode) {
//         super(e);
//         this.errorCode = errorCode;        
//     }
//     
//     // needed for use of error code in the JSF.
//     public int getErrorCode(){
//         return errorCode;
//     }
// }
// 
// Path: src/main/java/no/met/metadataeditor/datastore/MetadataRecords.java
// @XmlAccessorType(XmlAccessType.NONE)
// public static class ResourceMetadata {
// 
//     private String name;
//     private String relativeUrl;
//     private Date lastModified;
// 
//     public ResourceMetadata() {
//     }
// 
//     public ResourceMetadata(String name, String relativeUrl, Date lastMdified) {
//         this.name = name;
//         this.relativeUrl = relativeUrl;
//         this.lastModified = lastMdified;
//     }
// 
//     @XmlAttribute
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     @XmlAttribute
//     public String getRelativeUrl() {
//         return relativeUrl;
//     }
// 
//     public void setRelativeUrl(String relativeUrl) {
//         this.relativeUrl = relativeUrl;
//     }
// 
//     @XmlAttribute
//     @XmlJavaTypeAdapter(DateAdapter.class)
//     public Date getLastModified() {
//         return lastModified;
//     }
// 
//     public void setLastModified(Date lastModified) {
//         this.lastModified = lastModified;
//     }
// }


// Path: src/main/java/no/met/metadataeditor/datastore/DiskDataStore.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import no.met.metadataeditor.EditorException;
import no.met.metadataeditor.datastore.MetadataRecords.ResourceMetadata;
import org.apache.commons.io.FileUtils;


    @Override
    public boolean userHasWriteAccess(String username, String password) {

        // For the disk data store we assume that all user have access to write.
        return true;
    }

    @Override
    public String getDefaultUser(){
        return "";
    }

    @Override
    public String getDefaultPassword(){
        return "";
    }

    @Override
    public boolean delete(String url, String username, String password){
        return FileUtils.deleteQuietly(new File(url));
    }

    @Override
    public List<MetadataRecords.ResourceMetadata> listMetadataRecord() {
        //only allow .xml files
        Collection<File> files = FileUtils.listFiles(new File(metadataDirUrl()), new String[]{"xml"}, false);

        List<ResourceMetadata> records = new ArrayList<>();
        for(File f : files){
            records.add(new ResourceMetadata(f.getName(), f.getPath(), new Date(f.lastModified())));