// Path: src/main/java/es/tid/fiware/iot/ac/model/Policy.java
// @Entity
// public class Policy {
// 
//     @Embeddable
//     public static class PolicyId implements Serializable {
//         private String id;
//         private String tenant;
//         
//         public PolicyId() {
//             
//         }
//         
//         public PolicyId(String tenant, String id) {
//             this.id = id;
//             this.tenant = tenant;
//         }
//         
//         public String getId() {
//             return id;
//         }
//         
//         public String getTenant() {
//             return tenant;
//         }
//     }
//     
//     @EmbeddedId
//     private PolicyId internalId;
//     private String subject;
// 
//     private String policy;
// 
//     public Policy() {
//         
//     }
// 
//     public Policy(String id, String tenant, String subject, String policy) {
//         this.internalId = new PolicyId(tenant, id);
//         this.subject = subject;
//         this.policy = policy;
//     }
// 
//     public String getId() {
//         return internalId.getId();
//     }
// 
//     public PolicyId getInternalId() {
//         return internalId;
//     }
// 
//     public void setInternalId(PolicyId id) {
//         internalId = id;
//     }
//     
//     public String getTenant() {
//         return internalId.getTenant();
//     }
// 
//     public String getSubject() {
//         return subject;
//     }
// 
//     public String getPolicy() {
//         return policy;
//     }
// 
// }


import es.tid.fiware.iot.ac.model.Policy;
import io.dropwizard.hibernate.AbstractDAO;
import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package es.tid.fiware.iot.ac.dao;

/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */


public class PolicyDAOHibernate extends AbstractDAO<Policy> implements PolicyDao  {
    private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDAOHibernate.class);

    public PolicyDAOHibernate(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public Policy createPolicy(Policy policy) {
        LOGGER.debug("Persisting policy [{}] for subject [{}]", policy.getId(), policy.getSubject());
        LOGGER.trace("Policy contents: {}", policy.getPolicy());
        return persist(policy);
    }

    @Override
    public Policy loadPolicy(String tenant, String subject, String id) {
        LOGGER.debug("Getting policy for tenant [{}] for subject [{}] and id [{}]", tenant, subject, id);
        
        return get(new Policy.PolicyId(tenant, id));
    }

    @Override
    public Collection<Policy> getPolicies(String tenant, String subject) {        
        Criteria ct = this.currentSession().createCriteria(Policy.class)
            .add( Restrictions.eq("internalId.tenant", tenant) )
            .add( Restrictions.eq("subject", subject));
        return list(ct);
    }

    @Override
    public Policy updatePolicy(Policy policy) {
        LOGGER.debug("Updating policy [{}] for subject [{}]", policy.getId(), policy.getSubject());
        LOGGER.trace("Policy contents: {}", policy.getPolicy());

// Path: src/main/java/es/tid/fiware/iot/ac/model/Policy.java
// @Entity
// public class Policy {
// 
//     @Embeddable
//     public static class PolicyId implements Serializable {
//         private String id;
//         private String tenant;
//         
//         public PolicyId() {
//             
//         }
//         
//         public PolicyId(String tenant, String id) {
//             this.id = id;
//             this.tenant = tenant;
//         }
//         
//         public String getId() {
//             return id;
//         }
//         
//         public String getTenant() {
//             return tenant;
//         }
//     }
//     
//     @EmbeddedId
//     private PolicyId internalId;
//     private String subject;
// 
//     private String policy;
// 
//     public Policy() {
//         
//     }
// 
//     public Policy(String id, String tenant, String subject, String policy) {
//         this.internalId = new PolicyId(tenant, id);
//         this.subject = subject;
//         this.policy = policy;
//     }
// 
//     public String getId() {
//         return internalId.getId();
//     }
// 
//     public PolicyId getInternalId() {
//         return internalId;
//     }
// 
//     public void setInternalId(PolicyId id) {
//         internalId = id;
//     }
//     
//     public String getTenant() {
//         return internalId.getTenant();
//     }
// 
//     public String getSubject() {
//         return subject;
//     }
// 
//     public String getPolicy() {
//         return policy;
//     }
// 
// }


// Path: src/main/java/es/tid/fiware/iot/ac/dao/PolicyDAOHibernate.java
import es.tid.fiware.iot.ac.model.Policy;
import io.dropwizard.hibernate.AbstractDAO;
import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

package es.tid.fiware.iot.ac.dao;

/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */


public class PolicyDAOHibernate extends AbstractDAO<Policy> implements PolicyDao  {
    private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDAOHibernate.class);

    public PolicyDAOHibernate(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public Policy createPolicy(Policy policy) {
        LOGGER.debug("Persisting policy [{}] for subject [{}]", policy.getId(), policy.getSubject());
        LOGGER.trace("Policy contents: {}", policy.getPolicy());
        return persist(policy);
    }

    @Override
    public Policy loadPolicy(String tenant, String subject, String id) {
        LOGGER.debug("Getting policy for tenant [{}] for subject [{}] and id [{}]", tenant, subject, id);
        
        return get(new Policy.PolicyId(tenant, id));
    }

    @Override
    public Collection<Policy> getPolicies(String tenant, String subject) {        
        Criteria ct = this.currentSession().createCriteria(Policy.class)
            .add( Restrictions.eq("internalId.tenant", tenant) )
            .add( Restrictions.eq("subject", subject));
        return list(ct);
    }

    @Override
    public Policy updatePolicy(Policy policy) {
        LOGGER.debug("Updating policy [{}] for subject [{}]", policy.getId(), policy.getSubject());
        LOGGER.trace("Policy contents: {}", policy.getPolicy());

        String hql = "UPDATE Policy SET policy = :policy "  + 