package org.cryptonit.cloud.timestamping;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.cryptonit.cloud.ExecutionContext;
import org.cryptonit.cloud.interfaces.TimestampingPolicy;
import org.cryptonit.cloud.interfaces.TimestampingPolicyFactory;

public class PolicyFactory implements TimestampingPolicyFactory {
    ExecutionContext context;

    public PolicyFactory(ExecutionContext context) {
        this.context = context;
    }

    @Override
    public TimestampingPolicy getTimestampingPolicy(String domain) {
        TimestampingPolicy r = null;

        try {
            Connection c = context.database.getConnection();
            PreparedStatement ps = c.prepareStatement("SELECT identityId, algorithm FROM timestamping_policy WHERE domain=? and policyId is NULL");
            ps.setString(1, domain);
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                r = new Policy(rs.getString(1), null, rs.getString(2));
            }
        } catch (Exception e) {
            // TODO: Log error
        }

        return r;
    }

    @Override
    public TimestampingPolicy getTimestampingPolicy(String domain, ASN1ObjectIdentifier policyId) {
        return null;
    }    

    @Override
    public void addTimestampingPolicy(TimestampingPolicy policy, String domain) throws Exception {
        Connection c = context.database.getConnection();
        CallableStatement cs = c.prepareCall("INSERT INTO timestamping_policy(domain, policyId, identityId, algorithm, created) "
                + "VALUES (?, NULL, ?, ?, NOW())");
        cs.setString(1, domain);
        cs.setString(2, policy.getIdentity());
        cs.setString(3, policy.getAlgorithm());
        cs.execute();
    }

    @Override
    public void addTimestampingPolicy(TimestampingPolicy policy, String domain, ASN1ObjectIdentifier policyId) throws Exception {
        Connection c = context.database.getConnection();
        CallableStatement cs = c.prepareCall("INSERT INTO timestamping_policy(domain, policyId, identityId, algorithm, created) "
                + "VALUES (?, ?, ?, ?, NOW())");
        cs.setString(1, domain);
        cs.setString(2, policyId.toString());
        cs.setString(3, policy.getIdentity());
        cs.setString(4, policy.getAlgorithm());
        cs.execute();
    }
}

@@ -1338,19 +1338,615 @@
     
-return null
+TimestampingPolicy r = null;%0A%0A        try %7B%0A            Connection c = context.database.getConnection();%0A            PreparedStatement ps = c.prepareStatement(%22SELECT identityId, policyId, algorithm FROM timestamping_policy WHERE domain=? and policyId=?%22);%0A            ps.setString(1, domain);%0A            ps.setString(2, policyId.toString());%0A            ResultSet rs = ps.executeQuery();%0A%0A            if (rs.next()) %7B%0A                r = new Policy(rs.getString(1), rs.getString(2), rs.getString(3));%0A            %7D%0A        %7D catch (Exception e) %7B%0A            // TODO: Log error%0A        %7D%0A%0A        return r
 ;%0A  

Implement second getTimestampingPolicy method