Salome HOME
Improves file management
[modules/gde.git] / projects / GDE_App / GDE-ejb / src / java / com / edf / gde / dao / impl / ChunkDaoImpl.java
1 /*
2  * (C) 2015 EDF
3  */
4 package com.edf.gde.dao.impl;
5
6 import com.edf.gde.dao.ChunkDao;
7 import com.edf.gde.entities.Chunk;
8 import com.edf.gde.entities.GDEFile;
9 import com.edf.gde.transferables.ChunkTO;
10 import javax.persistence.EntityManager;
11
12 /**
13  *
14  * @author Kavoos
15  */
16 public class ChunkDaoImpl implements ChunkDao {
17
18     private final EntityManager em;
19
20     public ChunkDaoImpl(EntityManager em) {
21         this.em = em;
22     }
23
24     @Override
25     public ChunkTO createChunk(ChunkTO cto) {
26         Chunk c = Chunk.fromChunkTO(cto);
27         GDEFile file = em.find(GDEFile.class, cto.getFileId());
28         if (file == null) {
29             throw new RuntimeException("Invalid file id");
30         }
31         if (file.isValid()) {
32             throw new RuntimeException("Cannot add chunk to validated file");
33         }
34         em.persist(c);
35         return c.toChunkTO();
36     }
37
38     @Override
39     public void deleteChunk(ChunkTO cto) {
40         Chunk c = Chunk.fromChunkTO(cto);
41         em.remove(c);
42     }
43
44     @Override
45     public ChunkTO updateChunk(ChunkTO cto) {
46         Chunk c = Chunk.fromChunkTO(cto);
47         Chunk up = em.merge(c);
48         return up.toChunkTO();
49     }
50
51     @Override
52     public ChunkTO findChunk(ChunkTO cto) {
53         Chunk found = em.find(Chunk.class, cto.getId());
54         return found.toChunkTO();
55     }
56
57     @Override
58     public ChunkTO findById(long id) {
59         Chunk found = (Chunk) em.createNamedQuery("Chunk.findById")
60                 .setParameter("id", id)
61                 .getSingleResult();
62         return found.toChunkTO();
63     }
64
65     @Override
66     public ChunkTO findByFileId(long fileId) {
67         Chunk found = (Chunk) em.createNamedQuery("Chunk.findByFileId")
68                 .setParameter("fileId", fileId)
69                 .getSingleResult();
70         return found.toChunkTO();
71     }
72
73 }