Coverage Report - br.mia.test.model.dao.DAOBussinessRule
 
Classes in this File Line Coverage Branch Coverage Complexity
DAOBussinessRule
0%
0/432
0%
0/78
0
 
 1  
 package br.mia.test.model.dao;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.sql.Connection;
 5  
 import java.sql.PreparedStatement;
 6  
 import java.sql.ResultSet;
 7  
 import java.sql.SQLException;
 8  
 import java.util.ArrayList;
 9  
 import java.util.List;
 10  
 
 11  
 import org.hibernate.Query;
 12  
 import org.hibernate.Session;
 13  
 
 14  
 import br.mia.test.model.CasoUso;
 15  
 import br.mia.test.model.RegraNegocio;
 16  
 import br.mia.test.model.RegraNegocioStep;
 17  
 import br.mia.test.model.exception.NomeTamanhoZeroException;
 18  
 import br.mia.test.model.exception.RegraNulaException;
 19  
 import br.mia.test.testcase.DadosTeste;
 20  
 import br.mia.test.view.Global;
 21  
 
 22  
 /***
 23  
  * Classe DAO para regra de neg�cio
 24  
  *
 25  
  * @author 74397176353
 26  0
  *
 27  
  */
 28  0
 public class DAOBussinessRule
 29  
 {
 30  
 
 31  0
     public void deleteStepRule(final RegraNegocioStep step)
 32  
             throws InstantiationException, IllegalAccessException,
 33  0
             ClassNotFoundException, SQLException, IOException {
 34  0
 
 35  0
         Session session = HibernateUtil.getSession();
 36  0
 
 37  0
         session.beginTransaction();
 38  0
 
 39  0
         session.delete(step);
 40  0
 
 41  0
         session.getTransaction().commit();
 42  0
     }
 43  
 
 44  
     public void carregaGlobalRule(CasoUso useCase)
 45  
             throws InstantiationException,
 46  
 
 47  0
             IllegalAccessException, ClassNotFoundException, SQLException,
 48  
             IOException {
 49  0
 
 50  0
         Session session = HibernateUtil.getSession();
 51  0
 
 52  0
         session.beginTransaction();
 53  0
 
 54  0
         Query query = session
 55  0
                 .createQuery("from RegraNegocio  as r where r.casoUso.id=:idUseCase");
 56  
 
 57  0
         query.setInteger("idUseCase", useCase.getId());
 58  0
 
 59  0
         List<RegraNegocio> rules = query.list();
 60  0
 
 61  0
         for (RegraNegocio regraNegocio : rules) {
 62  0
             if (!(useCase.getRuleMapaGlobal().containsKey(regraNegocio
 63  0
                     .getName()
 64  0
                     + useCase.getName()))) {
 65  0
                 useCase.getRuleMapaGlobal().put(
 66  0
                         regraNegocio.getName() + useCase.getName(),
 67  0
                         regraNegocio);
 68  0
             }
 69  
 
 70  0
         }
 71  0
 
 72  0
     }
 73  0
 
 74  
     /***
 75  0
      * Insere DAdo na regra de neg�cio
 76  
      *
 77  0
      * @param data
 78  
      * @param step
 79  0
      * @return
 80  0
      * @throws InstantiationException
 81  0
      * @throws IllegalAccessException
 82  
      * @throws ClassNotFoundException
 83  0
      * @throws SQLException
 84  0
      * @throws IOException
 85  0
      */
 86  
     public void insertDadoBussinesRule(DadosTeste data, RegraNegocioStep step)
 87  0
             throws InstantiationException, IllegalAccessException,
 88  0
             ClassNotFoundException, SQLException, IOException {
 89  0
 
 90  0
         Session session = HibernateUtil.getSession();
 91  0
 
 92  0
         session.beginTransaction();
 93  
 
 94  0
         step.getDatas().add(data);
 95  0
 
 96  0
         session.update(step);
 97  0
 
 98  0
         session.getTransaction().commit();
 99  0
     }
 100  0
 
 101  
     public void deleteDadosStep(DadosTeste data, RegraNegocioStep step)
 102  0
             throws InstantiationException, IllegalAccessException,
 103  
             ClassNotFoundException, SQLException, IOException {
 104  0
 
 105  0
         Session session = HibernateUtil.getSession();
 106  
 
 107  0
         session.beginTransaction();
 108  
 
 109  0
         step.getDatas().remove(data);
 110  
 
 111  0
         session.getTransaction().commit();
 112  
 
 113  0
         session.flush();
 114  0
     }
 115  
 
 116  
     /***
 117  
      * Seleciona dados de uma passo de uma regra de neg�cio
 118  
      */
 119  
     public List<DadosTeste> selectDadosStep(RegraNegocioStep valorObject,
 120  
             CasoUso useCase) throws InstantiationException,
 121  0
             IllegalAccessException, ClassNotFoundException, SQLException,
 122  0
             IOException {
 123  0
 
 124  0
         return valorObject.getDatas();
 125  0
 
 126  
     }
 127  0
 
 128  0
     public void updateIDBussinessRule(int id1, int id2)
 129  
             throws InstantiationException, IllegalAccessException,
 130  0
             ClassNotFoundException, SQLException, IOException {
 131  0
         Connection con = Singleton.getConnection();
 132  0
 
 133  0
         PreparedStatement st = con
 134  0
                 .prepareStatement("update stepbussinessrule set id=0 where id=?");
 135  0
 
 136  0
         st.setInt(1, id1);
 137  0
         st.executeUpdate();
 138  0
 
 139  0
         st = con
 140  0
                 .prepareStatement("update stepbussinessrule set id=? where id=?");
 141  0
 
 142  0
         st.setInt(1, id1);
 143  0
         st.setInt(2, id2);
 144  
 
 145  0
         st.executeUpdate();
 146  
 
 147  0
         st = con
 148  0
                 .prepareStatement("update stepbussinessrule set id=? where id=0");
 149  
 
 150  0
         st.setInt(1, id2);
 151  0
         st.executeUpdate();
 152  
 
 153  0
     }
 154  
 
 155  0
     public void updateBussinessRule(List<RegraNegocio> list, CasoUso useCase)
 156  0
             throws InstantiationException, IllegalAccessException,
 157  
             ClassNotFoundException, SQLException, IOException {
 158  0
 
 159  0
         Session session = HibernateUtil.getSession();
 160  0
         session.beginTransaction();
 161  0
         if (list != null) {
 162  0
             for (RegraNegocio regraNegocio : list) {
 163  0
                 session.update(regraNegocio);
 164  
 
 165  
             }
 166  
 
 167  
         }
 168  
 
 169  0
         session.getTransaction().commit();
 170  0
         session.flush();
 171  
 
 172  0
     }
 173  
 
 174  0
     public void update(RegraNegocio rule, CasoUso useCase)
 175  0
             throws InstantiationException, IllegalAccessException,
 176  
             ClassNotFoundException, SQLException, IOException,
 177  0
             RegraNulaException, NomeTamanhoZeroException {
 178  0
 
 179  0
         Session session = HibernateUtil.getSession();
 180  0
 
 181  0
         session.beginTransaction();
 182  0
 
 183  0
         session.update(rule);
 184  
 
 185  0
         session.getTransaction().commit();
 186  
 
 187  0
         session.flush();
 188  
 
 189  0
     }
 190  
 
 191  
     public List<RegraNegocioStep> selectSteps(RegraNegocio rule, CasoUso useCase)
 192  
             throws InstantiationException, IllegalAccessException,
 193  
             ClassNotFoundException, SQLException, IOException {
 194  0
 
 195  0
         return rule.getSteps();
 196  0
     }
 197  0
 
 198  
     public List<RegraNegocio> selectBussinessRule(CasoUso useCase)
 199  0
             throws InstantiationException,
 200  0
 
 201  
             IllegalAccessException, ClassNotFoundException, SQLException,
 202  0
             IOException {
 203  0
 
 204  0
         return useCase.getRegras();
 205  0
 
 206  0
     }
 207  
 
 208  0
     public ArrayList<RegraNegocio> selectBussinessRuleGlobal(CasoUso useCase)
 209  
             throws InstantiationException,
 210  0
 
 211  0
             IllegalAccessException, ClassNotFoundException, SQLException,
 212  
             IOException {
 213  0
         Connection con = Singleton.getConnection();
 214  0
 
 215  0
         ArrayList<RegraNegocio> rules = new ArrayList<RegraNegocio>();
 216  0
 
 217  0
         PreparedStatement st = con
 218  0
                 .prepareStatement("Select id, nome,rule,reuse,idOld from bussinessruleglobal");
 219  
 
 220  0
         ResultSet rs = st.executeQuery();
 221  
 
 222  0
         while (rs.next()) {
 223  0
             String nome = rs.getString(2);
 224  0
             int id = rs.getInt(1);
 225  0
             String card = rs.getString(3);
 226  0
             boolean reuse = rs.getBoolean(4);
 227  0
             int idOld = rs.getInt(5);
 228  
 
 229  0
             RegraNegocio rule = new RegraNegocio();
 230  
 
 231  0
             rule.setId(id);
 232  0
             rule.setName(nome);
 233  0
             rule.setRule(card);
 234  0
             rule.setReuse(reuse);
 235  0
             rule.setIdOld(idOld);
 236  0
 
 237  0
             if (!(Global.getRuleMapaGlobal().containsKey(rule.getName()))) {
 238  0
                 useCase.getRuleMapaGlobal().put(rule.getName(), rule);
 239  0
             }
 240  0
 
 241  0
             rules.add(rule);
 242  0
         }
 243  0
 
 244  0
         return rules;
 245  
     }
 246  0
 
 247  
     public void deleteRuleGlobal(RegraNegocio rule)
 248  0
             throws InstantiationException, IllegalAccessException,
 249  
             ClassNotFoundException, SQLException, IOException {
 250  0
         Connection con = Singleton.getConnection();
 251  
 
 252  0
         PreparedStatement st = con
 253  0
                 .prepareStatement("delete from bussinessruleglobal where id=? ");
 254  
 
 255  0
         st.setInt(1, rule.getId());
 256  0
         st.executeUpdate();
 257  0
 
 258  0
     }
 259  0
 
 260  0
     public void deleteRule(RegraNegocio rule, CasoUso useCase)
 261  0
             throws InstantiationException, IllegalAccessException,
 262  
             ClassNotFoundException, SQLException, IOException {
 263  0
 
 264  0
         DAOBussinessRule daoBussinessRule = new DAOBussinessRule();
 265  0
         for (RegraNegocioStep step : rule.getSteps()) {
 266  0
             daoBussinessRule.deleteStepRule(step);
 267  0
         }
 268  0
 
 269  0
         Session session = HibernateUtil.getSession();
 270  
 
 271  0
         session.beginTransaction();
 272  0
 
 273  0
         session.delete(rule);
 274  
 
 275  0
         session.getTransaction().commit();
 276  0
 
 277  0
         session.flush();
 278  0
     }
 279  
 
 280  0
     public RegraNegocio insert(RegraNegocio rule, CasoUso useCase)
 281  0
             throws SQLException, InstantiationException,
 282  0
             IllegalAccessException, ClassNotFoundException, IOException,
 283  
             RegraNulaException, NomeTamanhoZeroException {
 284  0
         if (rule != null) {
 285  0
             if (rule.getName().length() > 0) {
 286  0
 
 287  0
                 Session session = HibernateUtil.getSession();
 288  0
 
 289  0
                 session.beginTransaction();
 290  0
 
 291  0
                 rule.setCasoUso(useCase);
 292  0
 
 293  0
                 session.save(rule);
 294  0
 
 295  0
                 session.getTransaction().commit();
 296  
 
 297  0
                 return rule;
 298  
             } else {
 299  0
                 throw new NomeTamanhoZeroException();
 300  
             }
 301  0
         } else {
 302  0
             throw new RegraNulaException();
 303  0
         }
 304  
     }
 305  
 
 306  0
     public RegraNegocio insertGlobal(RegraNegocio rule, CasoUso useCase)
 307  0
             throws SQLException, InstantiationException,
 308  
             IllegalAccessException, ClassNotFoundException, IOException {
 309  0
         Connection con = Singleton.getConnection();
 310  
 
 311  0
         PreparedStatement st = con
 312  0
                 .prepareStatement("Insert into bussinessruleglobal(nome,rule,idUseCase,reuse,idOld) values (?,?,?,?,?)");
 313  
 
 314  0
         st.setString(1, rule.getName());
 315  0
         st.setString(2, rule.getRule());
 316  0
         st.setInt(3, useCase.getId());
 317  0
         st.setBoolean(4, rule.isReuse());
 318  0
         st.setInt(5, rule.getId());
 319  0
         st.executeUpdate();
 320  
 
 321  0
         st = con
 322  0
                 .prepareStatement("select id from bussinessrule where nome=? and rule=? and idUseCase=?");
 323  0
         st.setString(1, rule.getName());
 324  0
         st.setString(2, rule.getRule());
 325  0
         st.setInt(3, useCase.getId());
 326  0
 
 327  0
         ResultSet rs = st.executeQuery();
 328  0
 
 329  0
         rs.next();
 330  0
 
 331  0
         int id = rs.getInt(1);
 332  0
         rule.setId(id);
 333  0
 
 334  0
         return rule;
 335  
     }
 336  
 
 337  
     public RegraNegocioStep insert(RegraNegocio rule, RegraNegocioStep step)
 338  
             throws InstantiationException, IllegalAccessException,
 339  
             ClassNotFoundException, SQLException, IOException {
 340  
 
 341  0
         Session session = HibernateUtil.getSession();
 342  0
 
 343  0
         session.beginTransaction();
 344  
 
 345  0
         step.setRegra(rule);
 346  
 
 347  0
         session.save(step);
 348  0
 
 349  0
         session.getTransaction().commit();
 350  
 
 351  0
         session.flush();
 352  
 
 353  0
         return step;
 354  0
 
 355  0
     }
 356  0
 
 357  0
     public RegraNegocioStep update(RegraNegocio rule, RegraNegocioStep step)
 358  0
             throws InstantiationException, IllegalAccessException,
 359  0
             ClassNotFoundException, SQLException, IOException {
 360  0
 
 361  0
         Session session = HibernateUtil.getSession();
 362  
 
 363  0
         session.beginTransaction();
 364  0
 
 365  0
         session.update(step);
 366  
 
 367  0
         session.getTransaction().commit();
 368  
 
 369  0
         session.flush();
 370  
 
 371  0
         return step;
 372  
 
 373  0
     }
 374  
 
 375  0
 }