Example #1
0
 private static void deleteWorkerFromAnywhere(String workerName) {
   Worker workerForDel = workerDAO.getByName(workerName);
   Department depForUpdate = departmentDAO.getByWorker(workerForDel);
   if (depForUpdate != null) {
     depForUpdate.removeWorker(workerForDel);
     departmentDAO.update(depForUpdate);
   }
   workerDAO.remove(workerForDel);
 }
Example #2
0
  public static Department getDepOrCreate(String departmentName) {
    Department dp = departmentDAO.getByName(departmentName);
    if (dp == null) {
      dp = new Department(departmentName);
      departmentDAO.save(dp);
    }

    return dp;
  }
Example #3
0
 private static void deleteDepartmentAndRelocateWorkersToNullDep(
     String departmentName, Department nullDepartment) {
   Department depForDel = departmentDAO.getByName(departmentName);
   if (depForDel != null) {
     nullDepartment.getWorkers().addAll(depForDel.getWorkers());
     departmentDAO.remove(depForDel);
     departmentDAO.update(nullDepartment);
   }
 }
Example #4
0
  public static void main(String[] args) {
    LOGGER.info(info);
    Department nullDepartment = departmentDAO.getByName("with out department");
    if (nullDepartment == null) {
      nullDepartment = new Department("with out department");
      departmentDAO.save(nullDepartment);
    }
    Department currentDepartment = null;

    try (BufferedReader reader =
        new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")))) {
      while (true) {
        String line = reader.readLine();
        if (line != null) {
          if (line.equals("exit")) {
            break;
          } else if (line.startsWith("dep")) {
            String departmentName = line.substring(4, line.length());
            if (isNameOk(departmentName)) {
              currentDepartment = getDepOrCreate(departmentName);
            }
          } else if (line.startsWith("work")) {
            String workerName = line.substring(5, line.length());
            if (isNameOk(workerName)) {
              Worker newWorker = getWorkOrCreate(currentDepartment, nullDepartment, workerName);
            }
          } else if (line.startsWith("del_dep")) {
            String departmentName = line.substring(8, line.length());
            if (isNameOk(departmentName)) {
              deleteDepartmentAndRelocateWorkersToNullDep(departmentName, nullDepartment);
            }
          } else if (line.startsWith("del_work_dep")) {
            String workerName = line.substring(13, line.length());
            if (isNameOk(workerName)) {
              deleteWorkerFromHisDepartmentAndRelocateHimToNullDep(workerName, nullDepartment);
            }
          } else if (line.startsWith("del_work")) {
            String workerName = line.substring(9, line.length());
            if (isNameOk(workerName)) {
              deleteWorkerFromAnywhere(workerName);
            }

          } else {
            repeatInfoMessage();
          }
        }
      }
    } catch (IOException e) {
      LOGGER.error("readLine exception", e);
    }

    LOGGER.info(workerDAO.getAll().toString());
    LOGGER.info(departmentDAO.getAll().toString());
  }
Example #5
0
 private static void deleteWorkerFromHisDepartmentAndRelocateHimToNullDep(
     String workerName, Department nullDepartment) {
   Worker workerForDel = workerDAO.getByName(workerName);
   Department depForUpdate = departmentDAO.getByWorker(workerForDel);
   if (depForUpdate != null) {
     depForUpdate.removeWorker(workerForDel);
     nullDepartment.addWorker(workerForDel);
     departmentDAO.update(depForUpdate);
     departmentDAO.update(nullDepartment);
   }
 }
Example #6
0
  private static Worker getWorkOrCreate(
      Department currentDepartment, Department nullDepartment, String workerName) {
    Worker w = workerDAO.getByName(workerName);
    if (w == null) {
      w = new Worker(workerName);
      workerDAO.save(w);
    } else {
      Department oldWorkerDep = departmentDAO.getByWorker(w);
      if (oldWorkerDep != null) {
        oldWorkerDep.removeWorker(w);
        departmentDAO.update(oldWorkerDep);
      }
    }

    if (currentDepartment != null) {
      currentDepartment.addWorker(w);
      departmentDAO.update(currentDepartment);
    } else {
      nullDepartment.addWorker(w);
      departmentDAO.update(nullDepartment);
    }

    return w;
  }