package org.devilry.core.session.dao;

import org.devilry.core.entity.Delivery;
import org.devilry.core.entity.FileMeta;
import org.devilry.core.entity.DeliveryCandidate;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class DeliveryCandidateImpl implements DeliveryCandidateRemote {

	@PersistenceContext(unitName = "DevilryCore")
	protected EntityManager em;
		
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long create(long deliveryId) {
		
		Delivery delivery = em.find(Delivery.class, deliveryId);
		
		DeliveryCandidate candidate = new DeliveryCandidate();
		// Set parent
		candidate.setDelivery(delivery);
		
		// Set time of delivery
		setTimeOfDelivery(candidate.getId(), new Date());
		
		em.persist(candidate);
		em.flush();
		
		return candidate.getId();
	}
	
	protected DeliveryCandidate getDeliveryCandidate(long deliveryCandidateId) {
		return em.find(DeliveryCandidate.class, deliveryCandidateId);
	}
	

	public long getDelivery(long deliveryCandidateId) {
		return getDeliveryCandidate(deliveryCandidateId).getDelivery().getId();
	}
	

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public long addFile(long deliveryCandidateId, String filePath) {
		FileMeta f = new FileMeta(getDeliveryCandidate(deliveryCandidateId), filePath);
		
		DeliveryCandidate deliveryCandidate = getDeliveryCandidate(deliveryCandidateId);
		f.setDeliveryCandidate(deliveryCandidate);
		
		em.persist(f);
		em.flush();
		return f.getId();
	}

	public List<Long> getFiles(long deliveryCandidateId) {
		Query q = em.createQuery("SELECT f.id FROM FileMeta f "
				+ "WHERE f.deliveryCandidate.id = :id ORDER BY f.filePath");
		q.setParameter("id", deliveryCandidateId);
		return q.getResultList();
	}


	public int getStatus(long deliveryCandidateId) {
		return getDeliveryCandidate(deliveryCandidateId).getStatus();
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public void setStatus(long deliveryCandidateId, int status) {
		getDeliveryCandidate(deliveryCandidateId).setStatus(status);
	}

	public Date getTimeOfDelivery(long deliveryCandidateId) {
		return getDeliveryCandidate(deliveryCandidateId).getTimeOfDelivery();
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public void setTimeOfDelivery(long deliveryCandidateId, Date timeOfDelivery) {
		getDeliveryCandidate(deliveryCandidateId).setTimeOfDelivery(timeOfDelivery);
	}
}
@@ -1450,37 +1450,11 @@
 %7B%0A%09%09
-FileMeta f = new FileMeta(get
+%0A%09%09
 Deli
@@ -1458,33 +1458,33 @@
 eliveryCandidate
-(
+ 
 deliveryCandidat
@@ -1488,29 +1488,14 @@
 date
-Id), filePath);%0A%09%09%0A%09%09
+ = get
 Deli
@@ -1499,33 +1499,33 @@
 eliveryCandidate
- 
+(
 deliveryCandidat
@@ -1517,36 +1517,90 @@
 eliveryCandidate
- = g
+Id);%0A%09%09%0A%09%09FileMeta fileMeta = new FileMeta();%0A%09%09fileMeta.s
 etDeliveryCandid
@@ -1624,57 +1624,46 @@
 date
-Id
 );%0A%09%09f
-.setDeliveryCandidate(deliveryCandidate
+ileMeta.setFilePath(filePath
 );
+%09%09
 %0A%09%09%0A
@@ -1676,16 +1676,23 @@
 ersist(f
+ileMeta
 );%0A%09%09em.
@@ -1710,16 +1710,23 @@
 return f
+ileMeta
 .getId()

add file was messed up