/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ellectra.ws.codefilemanager;

import java.util.List;
import javax.jws.WebService;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import org.ellectra.entity.CodeFileManager;


/**
 *
 * @author tdi
 */
@WebService()
@Stateless()
public class CodeFileManagerWS {
    @PersistenceUnit
    private EntityManagerFactory emf;
    
    public boolean AddNewFile(String type,String lang,String filename)
    {
            EntityManager em = null;
            em = emf.createEntityManager();
            
            
            CodeFileManager pCodeFile;
            pCodeFile = new CodeFileManager();
            pCodeFile.setCodetype(type);
            pCodeFile.setLanguage(lang);
            pCodeFile.setFilename(filename);
               
                
                
                try {
      
                    em.persist(pCodeFile);
                    
                } catch (Exception e) {               

                    return false;
                }
        return true;
    }
    
    public boolean DeleteFile(Long id)
    {
        assert emf != null;  //Make sure injection went through correctly.
        EntityManager em = null;
        List<CodeFileManager> l= em.createQuery("select c from CodeFileManager c where c.id").setParameter("id",id).getResultList();
        CodeFileManager deleted = (CodeFileManager)l.get(0);
  	em.remove(deleted);
        return true;
    }
    
    public List<CodeFileManager> GetAllFiles() {
        assert emf != null;  //Make sure injection went through correctly.
        EntityManager em = null;
        em = emf.createEntityManager();
        return em.createQuery("select c from CodeFileManager c ").getResultList();
    }
    
     public List<CodeFileManager> GetAllFilesForType(String type) {
        assert emf != null;  //Make sure injection went through correctly.
        EntityManager em = null;
        em = emf.createEntityManager();
        return em.createQuery("select c from CodeFileManager c where c.codetype=:uv").setParameter("uv",type).getResultList();
    }
}
