1 | |
package br.mia.test.model.dao; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.sql.Connection; |
5 | |
import java.sql.SQLException; |
6 | |
import java.util.ArrayList; |
7 | |
import java.util.List; |
8 | |
|
9 | |
import org.hibernate.Session; |
10 | |
import org.hibernate.Transaction; |
11 | |
import org.hibernate.criterion.Order; |
12 | |
import org.hibernate.criterion.Property; |
13 | |
|
14 | |
import br.mia.test.model.Build; |
15 | |
import br.mia.test.model.CasoUso; |
16 | 0 | import br.mia.test.model.Projeto; |
17 | |
import br.mia.test.model.exception.NomeTamanhoZeroException; |
18 | |
import br.mia.test.pessoas.Funcionario; |
19 | |
|
20 | 0 | public class DAOProject |
21 | 0 | { |
22 | 0 | public void delete(Projeto projeto) throws InstantiationException, |
23 | |
IllegalAccessException, ClassNotFoundException, SQLException, |
24 | 0 | IOException { |
25 | 0 | |
26 | 0 | DAOUseCase daoCasoUso = new DAOUseCase(); |
27 | 0 | for (CasoUso useCase : projeto.getCasosUso()) { |
28 | 0 | |
29 | 0 | daoCasoUso.deleteUseCase(useCase); |
30 | 0 | |
31 | 0 | } |
32 | 0 | |
33 | 0 | Session session = HibernateUtil.getSession(); |
34 | 0 | |
35 | 0 | session.beginTransaction(); |
36 | 0 | |
37 | 0 | session.delete(projeto); |
38 | 0 | |
39 | 0 | session.getTransaction().commit(); |
40 | 0 | |
41 | 0 | session.flush(); |
42 | 0 | } |
43 | 0 | |
44 | |
public int insert(Projeto projeto) throws SQLException, |
45 | 0 | InstantiationException, IllegalAccessException, |
46 | 0 | ClassNotFoundException, IOException, NomeTamanhoZeroException { |
47 | |
|
48 | 0 | Session session = HibernateUtil.getSession(); |
49 | 0 | |
50 | 0 | if (projeto.getNome().length() == 0) { |
51 | 0 | throw new NomeTamanhoZeroException(); |
52 | 0 | } |
53 | 0 | |
54 | 0 | Transaction tx = session.beginTransaction(); |
55 | 0 | |
56 | 0 | session.save(projeto); |
57 | 0 | |
58 | 0 | tx.commit(); |
59 | 0 | |
60 | 0 | session.flush(); |
61 | 0 | |
62 | 0 | Property nome = Property.forName("nome"); |
63 | 0 | |
64 | 0 | List<Projeto> projetos = session.createCriteria(Projeto.class).add( |
65 | 0 | nome.eq(projeto.getNome())).list(); |
66 | 0 | |
67 | 0 | projeto = (Projeto) projetos.get(0); |
68 | 0 | |
69 | 0 | int id = projeto.getId(); |
70 | 0 | |
71 | 0 | return id; |
72 | 0 | |
73 | 0 | } |
74 | 0 | |
75 | 0 | public int update(Projeto projeto) throws InstantiationException, |
76 | 0 | IllegalAccessException, ClassNotFoundException, SQLException, |
77 | 0 | IOException, NomeTamanhoZeroException { |
78 | 0 | Connection con = Singleton.getConnection(); |
79 | 0 | |
80 | 0 | int id = 0; |
81 | 0 | |
82 | 0 | if (projeto.getId() == 0) { |
83 | 0 | |
84 | 0 | id = this.insert(projeto); |
85 | 0 | |
86 | |
} else { |
87 | 0 | |
88 | 0 | Session session = HibernateUtil.getSession(); |
89 | 0 | |
90 | 0 | session.beginTransaction(); |
91 | 0 | |
92 | 0 | session.update(projeto); |
93 | 0 | |
94 | 0 | session.getTransaction().commit(); |
95 | 0 | |
96 | 0 | session.flush(); |
97 | 0 | |
98 | 0 | return projeto.getId(); |
99 | 0 | |
100 | |
} |
101 | 0 | |
102 | 0 | return id; |
103 | 0 | |
104 | |
} |
105 | 0 | |
106 | 0 | public List<Projeto> selectProjetos(Funcionario funcionario) |
107 | 0 | throws InstantiationException, IllegalAccessException, |
108 | 0 | ClassNotFoundException, SQLException, IOException { |
109 | 0 | |
110 | 0 | return funcionario.getProjetos(); |
111 | 0 | |
112 | 0 | } |
113 | 0 | |
114 | |
public ArrayList<Funcionario> selectUsuarios(Projeto projeto) |
115 | 0 | throws InstantiationException, IllegalAccessException, |
116 | |
ClassNotFoundException, SQLException, IOException { |
117 | 0 | |
118 | 0 | ArrayList<Funcionario> usuarios = new ArrayList<Funcionario>(); |
119 | 0 | |
120 | 0 | usuarios.addAll(projeto.getFuncionarios()); |
121 | 0 | |
122 | 0 | return usuarios; |
123 | 0 | |
124 | |
} |
125 | 0 | |
126 | |
public void deletarProjetos(Funcionario funcionario, Projeto projeto) |
127 | 0 | throws InstantiationException, IllegalAccessException, |
128 | |
ClassNotFoundException, SQLException, IOException { |
129 | 0 | |
130 | 0 | projeto.getFuncionarios().remove(funcionario); |
131 | 0 | funcionario.getProjetos().remove(projeto); |
132 | 0 | |
133 | 0 | Session session = HibernateUtil.getSession(); |
134 | 0 | session.beginTransaction(); |
135 | 0 | session.update(projeto); |
136 | 0 | session.update(funcionario); |
137 | 0 | |
138 | 0 | session.getTransaction().commit(); |
139 | 0 | session.flush(); |
140 | 0 | |
141 | 0 | } |
142 | 0 | |
143 | |
public void insertProjetos(Funcionario funcionario, Projeto projeto) |
144 | 0 | throws InstantiationException, IllegalAccessException, |
145 | |
ClassNotFoundException, SQLException, IOException { |
146 | 0 | |
147 | 0 | funcionario.getProjetos().add(projeto); |
148 | 0 | |
149 | 0 | Session session = HibernateUtil.getSession(); |
150 | 0 | session.beginTransaction(); |
151 | 0 | session.saveOrUpdate(funcionario); |
152 | 0 | |
153 | 0 | session.getTransaction().commit(); |
154 | 0 | session.flush(); |
155 | 0 | |
156 | 0 | } |
157 | 0 | |
158 | |
public void insertEscopo(Projeto projeto, CasoUso useCase, Build build) |
159 | 0 | throws InstantiationException, IllegalAccessException, |
160 | 0 | ClassNotFoundException, SQLException, IOException { |
161 | 0 | Session session = HibernateUtil.getSession(); |
162 | 0 | |
163 | 0 | session.beginTransaction(); |
164 | 0 | |
165 | 0 | build.getCasoUso().add(useCase); |
166 | 0 | |
167 | 0 | session.update(build); |
168 | 0 | |
169 | 0 | session.getTransaction().commit(); |
170 | 0 | |
171 | 0 | session.flush(); |
172 | 0 | |
173 | 0 | } |
174 | 0 | |
175 | 0 | public void deleteEscopo(Projeto projeto, CasoUso useCase, Build build) |
176 | |
throws InstantiationException, IllegalAccessException, |
177 | 0 | ClassNotFoundException, SQLException, IOException { |
178 | 0 | |
179 | 0 | Session session = HibernateUtil.getSession(); |
180 | 0 | |
181 | 0 | session.beginTransaction(); |
182 | 0 | |
183 | 0 | build.getCasoUso().remove(useCase); |
184 | 0 | |
185 | 0 | session.update(build); |
186 | 0 | |
187 | 0 | session.getTransaction().commit(); |
188 | 0 | |
189 | 0 | session.flush(); |
190 | 0 | } |
191 | 0 | |
192 | |
public List<CasoUso> selectEscopo(Projeto projeto, Build build) |
193 | 0 | throws InstantiationException, IllegalAccessException, |
194 | 0 | ClassNotFoundException, SQLException, IOException { |
195 | 0 | return build.getCasoUso(); |
196 | 0 | |
197 | |
} |
198 | 0 | |
199 | 0 | public List<Projeto> selectProjetos() throws InstantiationException, |
200 | 0 | IllegalAccessException, ClassNotFoundException, SQLException, |
201 | 0 | IOException { |
202 | 0 | |
203 | 0 | Session session = HibernateUtil.getSession(); |
204 | 0 | |
205 | 0 | session.beginTransaction(); |
206 | 0 | |
207 | 0 | List<Projeto> listaProjetos = session.createCriteria(Projeto.class) |
208 | 0 | .addOrder(Order.asc("nome")).list(); |
209 | 0 | |
210 | 0 | return listaProjetos; |
211 | 0 | |
212 | |
} |
213 | 0 | |
214 | |
public Projeto selectProjetos(int id) throws InstantiationException, |
215 | 0 | IllegalAccessException, ClassNotFoundException, SQLException, |
216 | 0 | IOException { |
217 | |
|
218 | 0 | Session session = HibernateUtil.getSession(); |
219 | 0 | |
220 | 0 | Projeto projeto = (Projeto) session.load(Projeto.class, id); |
221 | 0 | |
222 | 0 | return projeto; |
223 | 0 | |
224 | 0 | } |
225 | |
|
226 | 0 | } |