/*
 * This file is part of dependency-check-core.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 */
package org.owasp.dependencycheck.data.nvdcve;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.TreeMap;
import org.owasp.dependencycheck.data.update.nvd.NvdCveInfo;
import org.owasp.dependencycheck.data.update.exception.UpdateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This is a wrapper around a set of properties that are stored in the database.
 *
 * @author Jeremy Long
 */
public class DatabaseProperties {

    /**
     * The Logger.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseProperties.class);
    /**
     * Modified key word, used as a key to store information about the modified file (i.e. the containing the last 8 days of
     * updates)..
     */
    public static final String MODIFIED = "Modified";
    /**
     * The properties file key for the last checked field - used to store the last check time of the Modified NVD CVE xml file.
     */
    public static final String LAST_CHECKED = "NVD CVE Checked";
    /**
     * The properties file key for the last updated field - used to store the last updated time of the Modified NVD CVE xml file.
     */
    public static final String LAST_UPDATED = "NVD CVE Modified";
    /**
     * Stores the last updated time for each of the NVD CVE files. These timestamps should be updated if we process the modified
     * file within 7 days of the last update.
     */
    public static final String LAST_UPDATED_BASE = "NVD CVE ";
    /**
     * The key for the last time the CPE data was updated.
     */
    public static final String LAST_CPE_UPDATE = "LAST_CPE_UPDATE";
    /**
     * The key for the database schema version.
     */
    public static final String VERSION = "version";

    /**
     * A collection of properties about the data.
     */
    private final Properties properties;
    /**
     * A reference to the database.
     */
    private final CveDB cveDB;

    /**
     * Constructs a new data properties object.
     *
     * @param cveDB the database object holding the properties
     */
    DatabaseProperties(CveDB cveDB) {
        this.cveDB = cveDB;
        this.properties = cveDB.getProperties();
    }

    /**
     * Returns whether or not any properties are set.
     *
     * @return whether or not any properties are set
     */
    public boolean isEmpty() {
        return properties == null || properties.isEmpty();
    }

    /**
     * Saves the last updated information to the properties file.
     *
     * @param updatedValue the updated NVD CVE entry
     * @throws UpdateException is thrown if there is an update exception
     */
    public void save(NvdCveInfo updatedValue) throws UpdateException {
        if (updatedValue == null) {
            return;
        }
        save(LAST_UPDATED_BASE + updatedValue.getId(), String.valueOf(updatedValue.getTimestamp()));
    }

    /**
     * Saves the key value pair to the properties store.
     *
     * @param key the property key
     * @param value the property value
     * @throws UpdateException is thrown if there is an update exception
     */
    public void save(String key, String value) throws UpdateException {
        properties.put(key, value);
        cveDB.saveProperty(key, value);
    }

    /**
     * Returns the property value for the given key. If the key is not contained in the underlying properties null is returned.
     *
     * @param key the property key
     * @return the value of the property
     */
    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    /**
     * Returns the property value for the given key. If the key is not contained in the underlying properties the default value is
     * returned.
     *
     * @param key the property key
     * @param defaultValue the default value
     * @return the value of the property
     */
    public String getProperty(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

    /**
     * Returns the collection of Database Properties as a properties collection.
     *
     * @return the collection of Database Properties
     */
    public Properties getProperties() {
        return properties;
    }

    /**
     * Returns a map of the meta data from the database properties. This primarily contains timestamps of when the NVD CVE
     * information was last updated.
     *
     * @return a map of the database meta data
     */
    public Map<String, String> getMetaData() {
        final Map<String, String> map = new TreeMap<String, String>();
        for (Entry<Object, Object> entry : properties.entrySet()) {
            final String key = (String) entry.getKey();
            if (!"version".equals(key)) {
                if (key.startsWith("NVD CVE ")) {
                    try {
                        final long epoch = Long.parseLong((String) entry.getValue());
                        final Date date = new Date(epoch);
                        final DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                        final String formatted = format.format(date);
                        map.put(key, formatted);
                    } catch (Throwable ex) { //deliberately being broad in this catch clause
                        LOGGER.debug("Unable to parse timestamp from DB", ex);
                        map.put(key, (String) entry.getValue());
                    }
                } else {
                    map.put(key, (String) entry.getValue());
                }
            }
        }
        return map;
    }
}

@@ -1459,16 +1459,23 @@
 modified
+%0A     *
  file (i
@@ -1511,23 +1511,16 @@
  days of
-%0A     *
  updates
@@ -1666,24 +1666,31 @@
 to store the
+%0A     *
  last check 
@@ -1882,24 +1882,31 @@
 to store the
+%0A     *
  last update
@@ -2100,16 +2100,23 @@
 s. These
+%0A     *
  timesta
@@ -2159,31 +2159,24 @@
 the modified
-%0A     *
  file within
@@ -2177,16 +2177,23 @@
 within 7
+%0A     *
  days of
@@ -4137,32 +4137,39 @@
 is not contained
+%0A     *
  in the underlyi
@@ -4474,16 +4474,23 @@
 ontained
+%0A     *
  in the 
@@ -4531,23 +4531,16 @@
 value is
-%0A     *
  returne
@@ -5119,16 +5119,23 @@
 es. This
+%0A     *
  primari
@@ -5176,23 +5176,16 @@
  NVD CVE
-%0A     *
  informa
@@ -5193,24 +5193,31 @@
 ion was last
+%0A     *
  updated.%0A  
@@ -5781,16 +5781,66 @@
 epoch);%0A
+                        synchronized (date) %7B%0A    
         
@@ -5942,32 +5942,36 @@
                 
+   
+ 
 final String for
@@ -6016,32 +6016,36 @@
                 
+    
 map.put(key, for
@@ -6053,16 +6053,42 @@
 atted);%0A
+                        %7D%0A
         

fix proposed by Jacks - synchronizing SimpleDateFormat