// Path: src/main/java/org/cejug/hurraa/model/Equipment.java
// @Entity
// public class Equipment {
// 	
// 	@Id
// 	@GeneratedValue(strategy = GenerationType.IDENTITY)
// 	private Long id;
// 	
// 	@NotBlank
// 	@Column(name="serial_id")
// 	private String serialId;
// 	
// 	@NotNull
// 	@Column(name="end_of_warranty")
// 	private Date endOfWarranty;
// 
// 	@Column(name="last_maintenance")
// 	private Date lastMaintenance;
// 	
// 	@Column(name="maintenance_description")
// 	private String maintenanceDescription;
// 	
// 	@ManyToOne(optional=false)
// 	private EquipmentModel equipmentModel;
// 
// 	public Long getId() {
// 		return id;
// 	}
// 
// 	public void setId(Long id) {
// 		this.id = id;
// 	}
// 
// 	public String getSerialId() {
// 		return serialId;
// 	}
// 
// 	public void setSerialId(String serialId) {
// 		this.serialId = serialId;
// 	}
// 
// 	public Date getEndOfWarranty() {
// 		return endOfWarranty;
// 	}
// 
// 	public void setEndOfWarranty(Date endOfWarranty) {
// 		this.endOfWarranty = endOfWarranty;
// 	}
// 
// 	public Date getLastMaintenance() {
// 		return lastMaintenance;
// 	}
// 
// 	public void setLastMaintenance(Date lastMaintenance) {
// 		this.lastMaintenance = lastMaintenance;
// 	}
// 
// 	public String getMaintenanceDescription() {
// 		return maintenanceDescription;
// 	}
// 
// 	public void setMaintenanceDescription(String maintenanceDescription) {
// 		this.maintenanceDescription = maintenanceDescription;
// 	}
// 
// 	public EquipmentModel getEquipmentModel() {
// 		return equipmentModel;
// 	}
// 
// 	public void setEquipmentModel(EquipmentModel equipmentModel) {
// 		this.equipmentModel = equipmentModel;
// 	}
// 
// 	
// }
// 
// Path: src/main/java/org/cejug/hurraa/model/bean/EquipmentBean.java
// @Stateless
// public class EquipmentBean extends AbstractBean<Equipment>{
//     
// 	@PersistenceContext
//     private EntityManager entityManager;
//     
//     public EquipmentBean() {
//         super(Equipment.class);
//     }
//     
//     @Override
//     protected EntityManager getEntityManager() {
//         return entityManager;
//     }
//     
// }
// 
// Path: src/main/java/org/cejug/hurraa/model/bean/EquipmentModelBean.java
// @Stateless
// public class EquipmentModelBean extends AbstractBean<EquipmentModel> {
//     
//     @PersistenceContext
//     private EntityManager entityManager;
//     
//     public EquipmentModelBean() {
//         super(EquipmentModel.class);
//     }
// 
//     @Override
//     protected EntityManager getEntityManager() {
//         return entityManager;
//     }
//     
//     @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
//     public boolean isNameAvailable(String name){
//         TypedQuery<EquipmentModel> query = getEntityManager()
//                     .createNamedQuery( "FIND_BY_NAME", EquipmentModel.class );
//         query.setParameter("name", name);
//         return query.getResultList().isEmpty();
//     }
//     
//     @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
//     public boolean isEquipmentTypeInUse(EquipmentType equipmentType){
//         Query query = getEntityManager().createNamedQuery("EQUIPMENTTYPE_IN_USE" , EquipmentModel.class );
//         query.setParameter("equipmentType", equipmentType );
//         return query.getResultList().isEmpty();
//     }
// 
// }


import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.validator.SimpleMessage;
import br.com.caelum.vraptor.validator.Validator;
import java.text.DateFormat;
import java.util.ResourceBundle;
import javax.inject.Inject;
import javax.validation.Valid;
import org.cejug.hurraa.model.Equipment;
import org.cejug.hurraa.model.bean.EquipmentBean;
import org.cejug.hurraa.model.bean.EquipmentModelBean;
import org.cejug.hurraa.producer.ValidationMessages;
import org.cejug.hurraa.validation.Unique;
    
    @Path("form/{id}")
    public void form(Long id) {
        Equipment equipment = equipmentBean.findById(id);
        result.include( equipment );
        result.include("models" , equipmentModelBean.findAll() );
        
    }
    
    @Post("insert")
    public void insert(@Valid  
    		@Unique(propertyName = "serialId" , identityPropertyName="id" , entityClass =Equipment.class )
    		Equipment equipment, Validator validator ) {
    	
    	verifyIfSelectedEquipmentModel(equipment, validator);
    	validator.onErrorForwardTo( EquipmentController.class ).form();
    	equipmentBean.insert(equipment);
    	
        result.include("message", messagesBundle.getString("insert.success") );
        result.redirectTo(EquipmentController.class).list();
    }
    
    @Get("list")
    public void list() {
        result.include("equipments", equipmentBean.findAll());
    }
    
    @Post
    @Path("update")
    public void update(@Valid 
// Path: src/main/java/org/cejug/hurraa/model/Equipment.java
// @Entity
// public class Equipment {
// 	
// 	@Id
// 	@GeneratedValue(strategy = GenerationType.IDENTITY)
// 	private Long id;
// 	
// 	@NotBlank
// 	@Column(name="serial_id")
// 	private String serialId;
// 	
// 	@NotNull
// 	@Column(name="end_of_warranty")
// 	private Date endOfWarranty;
// 
// 	@Column(name="last_maintenance")
// 	private Date lastMaintenance;
// 	
// 	@Column(name="maintenance_description")
// 	private String maintenanceDescription;
// 	
// 	@ManyToOne(optional=false)
// 	private EquipmentModel equipmentModel;
// 
// 	public Long getId() {
// 		return id;
// 	}
// 
// 	public void setId(Long id) {
// 		this.id = id;
// 	}
// 
// 	public String getSerialId() {
// 		return serialId;
// 	}
// 
// 	public void setSerialId(String serialId) {
// 		this.serialId = serialId;
// 	}
// 
// 	public Date getEndOfWarranty() {
// 		return endOfWarranty;
// 	}
// 
// 	public void setEndOfWarranty(Date endOfWarranty) {
// 		this.endOfWarranty = endOfWarranty;
// 	}
// 
// 	public Date getLastMaintenance() {
// 		return lastMaintenance;
// 	}
// 
// 	public void setLastMaintenance(Date lastMaintenance) {
// 		this.lastMaintenance = lastMaintenance;
// 	}
// 
// 	public String getMaintenanceDescription() {
// 		return maintenanceDescription;
// 	}
// 
// 	public void setMaintenanceDescription(String maintenanceDescription) {
// 		this.maintenanceDescription = maintenanceDescription;
// 	}
// 
// 	public EquipmentModel getEquipmentModel() {
// 		return equipmentModel;
// 	}
// 
// 	public void setEquipmentModel(EquipmentModel equipmentModel) {
// 		this.equipmentModel = equipmentModel;
// 	}
// 
// 	
// }
// 
// Path: src/main/java/org/cejug/hurraa/model/bean/EquipmentBean.java
// @Stateless
// public class EquipmentBean extends AbstractBean<Equipment>{
//     
// 	@PersistenceContext
//     private EntityManager entityManager;
//     
//     public EquipmentBean() {
//         super(Equipment.class);
//     }
//     
//     @Override
//     protected EntityManager getEntityManager() {
//         return entityManager;
//     }
//     
// }
// 
// Path: src/main/java/org/cejug/hurraa/model/bean/EquipmentModelBean.java
// @Stateless
// public class EquipmentModelBean extends AbstractBean<EquipmentModel> {
//     
//     @PersistenceContext
//     private EntityManager entityManager;
//     
//     public EquipmentModelBean() {
//         super(EquipmentModel.class);
//     }
// 
//     @Override
//     protected EntityManager getEntityManager() {
//         return entityManager;
//     }
//     
//     @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
//     public boolean isNameAvailable(String name){
//         TypedQuery<EquipmentModel> query = getEntityManager()
//                     .createNamedQuery( "FIND_BY_NAME", EquipmentModel.class );
//         query.setParameter("name", name);
//         return query.getResultList().isEmpty();
//     }
//     
//     @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
//     public boolean isEquipmentTypeInUse(EquipmentType equipmentType){
//         Query query = getEntityManager().createNamedQuery("EQUIPMENTTYPE_IN_USE" , EquipmentModel.class );
//         query.setParameter("equipmentType", equipmentType );
//         return query.getResultList().isEmpty();
//     }
// 
// }


// Path: src/main/java/org/cejug/hurraa/controller/EquipmentController.java
import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.validator.SimpleMessage;
import br.com.caelum.vraptor.validator.Validator;
import java.text.DateFormat;
import java.util.ResourceBundle;
import javax.inject.Inject;
import javax.validation.Valid;
import org.cejug.hurraa.model.Equipment;
import org.cejug.hurraa.model.bean.EquipmentBean;
import org.cejug.hurraa.model.bean.EquipmentModelBean;
import org.cejug.hurraa.producer.ValidationMessages;
import org.cejug.hurraa.validation.Unique;

    
    @Path("form/{id}")
    public void form(Long id) {
        Equipment equipment = equipmentBean.findById(id);
        result.include( equipment );
        result.include("models" , equipmentModelBean.findAll() );
        
    }
    
    @Post("insert")
    public void insert(@Valid  
    		@Unique(propertyName = "serialId" , identityPropertyName="id" , entityClass =Equipment.class )
    		Equipment equipment, Validator validator ) {
    	
    	verifyIfSelectedEquipmentModel(equipment, validator);
    	validator.onErrorForwardTo( EquipmentController.class ).form();
    	equipmentBean.insert(equipment);
    	
        result.include("message", messagesBundle.getString("insert.success") );
        result.redirectTo(EquipmentController.class).list();
    }
    
    @Get("list")
    public void list() {
        result.include("equipments", equipmentBean.findAll());
    }
    
    @Post
    @Path("update")
    public void update(@Valid 
    		@Unique(propertyName = "serialId" , identityPropertyName="id" , entityClass =Equipment.class )