public void updateCompany(CompanyDTO dto) throws Exception { Company company = getCompany(dto.id); validate(dto); List<String> beneficiaryIds = company .getBeneficiaries() .stream() .map(beneficiary -> beneficiary.getId().toString()) .collect(Collectors.toList()); for (BeneficiaryDTO beneDTO : dto.beneficiaries) { Beneficiary bene = new Beneficiary(beneDTO.name, company); Integer id = beneDTO.id; if (id != null) { bene.setId(id); String idString = id.toString(); if (!beneDao.idExists(idString)) { throw new RequestException("beneficiary with id %s does not exist", id); } beneDao.update(bene); beneficiaryIds.remove(idString); } else { beneDao.create(bene); } } if (beneficiaryIds.size() > 0) { beneDao.deleteIds(beneficiaryIds); } company = JsonUtil.fromJson(dto.toJson(), Company.class); dao.update(company); }
public void createCompany(CompanyDTO dto) throws Exception { validate(dto); Boolean exists = dao.idExists(dto.id); if (exists) { throw new RequestException("Company with id %s already exists", dto.id); } Company company = JsonUtil.fromJson(dto.toJson(), Company.class); company.setCreatedAt(); dao.create(company); for (BeneficiaryDTO beneDTO : dto.beneficiaries) { beneDao.create(new Beneficiary(beneDTO.name, company)); } }
// Sélection de la BDD company par requête SQL public List<Company> getAll() { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = DriverManager.getConnection(URL, USER, PASSWORD); statement = connection.createStatement(); String sql = "SELECT * FROM company"; resultSet = statement.executeQuery(sql); List<Company> companyList = new ArrayList<Company>(); while (resultSet.next()) { Company company = Company.builder() .setId(resultSet.getLong("id")) .setName(resultSet.getString("name")) .build(); companyList.add(company); } return companyList; } catch (Exception e) { e.printStackTrace(); } finally { DaoUtils.closeAll(resultSet, statement, connection); } return null; }
@Test public void testCreateCompany() { CityInfo cityinfo = cf.getCityInformation(2700); Phone p1 = new Phone(23728394, "Freddy's Phone"); Phone p2 = new Phone(20962783, "Line's Phone"); Phone p3 = new Phone(48484848, "Taxi Nordsjl"); Company c = new Company( "testCompany", "testDescription", 12345678, 20, "testMarketValue", "*****@*****.**", new Address("testStreet", "testAdditionalInfo", cityinfo)); c.addPhoneToEntity(p1); c.addPhoneToEntity(p2); c.addPhoneToEntity(p3); cf.createCompany(c); Assert.assertNotNull(c.getId()); }
@Override public Company deleteCompany(Company comp) { EntityManager em = getEntityManager(); try { Company c = em.find(Company.class, comp.getId()); //insert exception handling here em.getTransaction().begin(); em.remove(c); em.getTransaction().commit(); return c; } finally { em.close(); } }
public JsonObject createCompanyObject(Company c) { JsonObject company = new JsonObject(); company.addProperty("id", c.getId()); company.addProperty("name", c.getName()); company.addProperty("description", c.getDescription()); company.addProperty("cvr", c.getCvr()); company.addProperty("email", c.getEmail()); company.addProperty("street", c.getAddress().getStreet()); company.addProperty("additionalinfo", c.getAddress().getAdditionalinfo()); company.addProperty("zipcode", c.getAddress().getCityInfo().getZip()); company.addProperty("city", c.getAddress().getCityInfo().getCity()); JsonArray phones = new JsonArray(); List<Phone> phs = c.getPhones(); for (Phone ph : phs) { JsonObject phone = new JsonObject(); phone.addProperty("number", ph.getNumber()); phone.addProperty("description", ph.getDescription()); phones.add(phone); } company.add("phones", phones); company.addProperty("numemployees", c.getNumEmployees()); company.addProperty("marketvalue", c.getMarketValue()); return company; }
@Test public void testGetCompanyByCvr() { Company company = cf.getCompany(12345678); Assert.assertEquals(12345678, company.getCvr()); }
public Company updateCompany(Company comp) { EntityManager em = getEntityManager(); try { Company updated = em.find(Company.class, comp.getId()); //insert exception handling here updated.setName(comp.getName()); updated.setAddress(comp.getAddress()); updated.setCvr(comp.getCvr()); updated.setDescription(comp.getDescription()); updated.setMarketValue(comp.getMarketValue()); updated.setNumOfEmployees(comp.getNumOfEmployees()); updated.setEmail(comp.getEmail()); updated.setPhones(comp.getPhones()); em.getTransaction().begin(); em.merge(updated); em.getTransaction().commit(); return updated; } finally { em.close(); } }