/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

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

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;

public class PolicyDAOHibernate extends AbstractDAO<Policy> implements PolicyDao  {

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

    @Override
    public Policy createPolicy(Policy policy) {
        return persist(policy);
    }

    @Override
    public Policy loadPolicy(String tenant, String subject, String 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 Collection<String> getSubjects(String tenant) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Policy updatePolicy(Policy policy) {
        String hql = "UPDATE Policy SET policy = :policy "  + 
             "WHERE tenant = :tenant and internalId.id = :id";
        
        Query query = this.currentSession().createQuery(hql);
        query.setParameter("policy", policy.getPolicy());
        query.setParameter("tenant", policy.getInternalId().getTenant());
        query.setParameter("id", policy.getInternalId().getId());
        int result = query.executeUpdate();

        System.out.println("The result of the update is " + result); 
        
        return policy;
    }

    @Override
    public Policy deletePolicy(Policy policy) {
        this.currentSession().delete(policy);
        
        return policy;
    }

    @Override
    public void deleteFromTenant(String tenant) {
        String hql = "DELETE FROM Policy "  + 
             "WHERE internalId.tenant = :tenant_id";
        
        Query query = this.currentSession().createQuery(hql);
        query.setParameter("tenant_id", tenant);
        int result = query.executeUpdate();
        
        System.out.println("The result of the delete tenant is " + result);
    }

    @Override
    public void deleteFromSubject(String tenant, String subject) {
        String hql = "DELETE FROM Policy "  + 
             "WHERE subject = :subject_id";
        
        Query query = this.currentSession().createQuery(hql);
        query.setParameter("subject_id", subject);
        int result = query.executeUpdate();

        System.out.println("The result of the delete subject is " + result);        
    }
    
}

@@ -1,223 +1,423 @@
-/*%0A * To change this license header, choose License Headers in
+package es.tid.fiware.iot.ac.dao;%0A%0A/*%0A * Telef%C3%B3nica Digital -
  Pro
-ject Properties.%0A * To change this template file, choose Tools %7C Templates%0A * and open the template in the editor.%0A */%0A%0Apackage es.tid.fiware.iot.ac.dao;
+duct Development and Innovation%0A *%0A * THIS CODE AND INFORMATION ARE PROVIDED %22AS IS%22 WITHOUT WARRANTY OF ANY KIND,%0A * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED%0A * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.%0A *%0A * Copyright (c) Telef%C3%B3nica Investigaci%C3%B3n y Desarrollo S.A.U.%0A * All rights reserved.%0A */
 %0A%0Aim
@@ -2150,87 +2150,8 @@
 );%0A%0A
-        System.out.println(%22The result of the update is %22 + result); %0A        %0A
     
@@ -2161,32 +2161,32 @@
  return policy;%0A
+
     %7D%0A%0A    @Over
@@ -2655,93 +2655,8 @@
 ();%0A
-        %0A        System.out.println(%22The result of the delete tenant is %22 + result);%0A
     
@@ -2948,24 +2948,24 @@
 , subject);%0A
+
         int 
@@ -2999,94 +2999,8 @@
 e();
-%0A%0A        System.out.println(%22The result of the delete subject is %22 + result);        
 %0A   

FIX Printlns on the code