Employee Details Creation
Hi,
My question is creating the user details in bulk . I have a data in a JSON format. Now JSON data integrated with the Joget app and creating the user details.
Problem:
We require to create the Organization details, department and grade. In the .JSON file already all information once data load from the .JSON want to create user details with the Organization and department and grade.
Organization,Department and Grade part is not creating I am sharing my code please tell me how to resolve this issues. Where to add the code to create the require details.
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.dao.FormDataDao;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FileUtil;
import org.joget.commons.util.FileManager;
import org.joget.commons.util.LogUtil;
import org.joget.commons.util.SetupManager;
import org.joget.apps.form.service.FileUtil;
import org.joget.directory.model.service.DirectoryManager;
import org.joget.directory.model.service.ExtDirectoryManager;
import org.joget.plugin.base.DefaultApplicationPlugin;
import org.joget.workflow.model.WorkflowActivity;
import org.joget.workflow.model.WorkflowAssignment;
import org.joget.workflow.model.WorkflowProcessResult;
import org.joget.workflow.model.service.WorkflowManager;
import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.util.HashSet;
import java.util.Set;
import org.joget.apps.form.model.Element;
import org.joget.commons.util.UuidGenerator;
import org.apache.commons.lang3.StringUtils;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.dao.FormDataDao;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FileUtil;
import org.joget.commons.util.FileManager;
import org.joget.commons.util.LogUtil;
import org.joget.commons.util.SetupManager;
import org.joget.directory.model.Role;
import org.joget.directory.model.User;
import org.joget.directory.dao.RoleDao;
import org.joget.directory.dao.UserDao;
import org.joget.directory.dao.DepartmentDao;
import org.joget.directory.dao.EmploymentDao;
import org.joget.directory.dao.GradeDao;
import org.joget.directory.dao.GroupDao;
import org.joget.directory.dao.OrganizationDao;
import org.joget.directory.model.Department;
import org.joget.directory.model.Employment;
import org.joget.directory.model.Grade;
import org.joget.directory.model.Group;
import org.joget.directory.model.Organization;
import org.joget.directory.model.service.DirectoryUtil;
import org.joget.directory.model.service.UserSecurity;
import org.json.simple.parser.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.joget.commons.util.StringUtil;
JSONParser parser = new JSONParser();
{
System.out.println("Please wait employee details is updating under process*:");
// File dir = new File("#envVariable.csvPath#");
// Object obj = parser.parse(new FileReader("D:\\LeaveJSON\\OIC_CARE_04-APR-21.JSON"));
Object obj = parser.parse(new FileReader("#envVariable.jsonPath#"));
JSONArray empdetails= (JSONArray)obj;
for (int i=0; i<empdetails.size(); i++)
{
JSONObject object = empdetails.get(i);
// String empid = object.get("EMPLOYEE_NUMBER").toString();
//String fname = object.get("FIRST_NAME").toString();
//String fullname = object.get("FULL_NAME").toString();
//System.out.println("Employee ID: " +fullname+" "+empid);
FormRowSet rowsFileMetaData = new FormRowSet();
FormRow row = new FormRow();
String uuid = UuidGenerator.getInstance().getUuid();
//************ Start the process to insert the values from JSON to app_fd_oic_employee_details table
FormDataDao formDataDao = (FormDataDao) AppUtil.getApplicationContext().getBean("formDataDao");
//row.setProperty("id",uuid);
row.setProperty("id", object.get("EMPLOYEE_NUMBER").toString());
row.setProperty("employee_ID", object.get("EMPLOYEE_NUMBER").toString());
row.setProperty("first_Name", object.get("FIRST_NAME").toString());
row.setProperty("middle_Name", object.get("MIDDLE_NAMES").toString());
row.setProperty("last_Name", object.get("LAST_NAME").toString());
row.setProperty("full_Name", object.get("FULL_NAME").toString());
row.setProperty("email_ID", object.get("EMAIL_ADDRESS").toString());
row.setProperty("dob", object.get("DATE_OF_BIRTH").toString());
row.setProperty("doj", object.get("DATE_OF_JOINING").toString());
row.setProperty("gender", object.get("GENDER").toString());
row.setProperty("marital_status", object.get("MARITAL_STATUS").toString());
row.setProperty("nationality", object.get("NATIONALITY").toString());
row.setProperty("passport_no", object.get("PASSPORT_NO").toString());
row.setProperty("passport_issue_date", object.get("PASSPORT_ISSUE_DATE").toString());
row.setProperty("passport_expiry_date", object.get("PASSPORT_VALID_UPTO").toString());
row.setProperty("visa_issue_date", object.get("VISA_ISSUE_DATE").toString());
row.setProperty("visa_expiry_date", object.get("VISA_EXPIRY_DATE").toString());
row.setProperty("location", object.get("EMP_LOCATION").toString());
row.setProperty("org_code", object.get("BUSINESS_UNIT").toString());
row.setProperty("company_name", object.get("COMPANY_NAME").toString());
row.setProperty("employee_Type", object.get("EMPLOYEE_TYPE").toString());
row.setProperty("department", object.get("DEPARTMENT").toString());
row.setProperty("designation", object.get("SKILL").toString());
row.setProperty("grade", object.get("GRADE").toString());
row.setProperty("contract_start_date", object.get("CONTRACT_START_DATE").toString());
row.setProperty("contract_end_date", object.get("CONTRACT_END_DATE").toString());
row.setProperty("mobile_number", object.get("MOBILE_NUMBER").toString());
row.setProperty("employee_status", object.get("EMPLOYEE_STATUS").toString());
row.setProperty("linemanager_empID", object.get("LINE_MGR_EMP_NUMBER").toString());
row.setProperty("linemanager_empName", object.get("LINE_MGR_EMP_NAME").toString());
row.setProperty("linemanager_emailID", object.get("LINE_MGR_EMP_EMAIL").toString());
row.setProperty("available_leaves", object.get("AVAILABLE_LEAVES").toString());
row.setProperty("eligible_leaves", object.get("ELIGIBLE_LEAVES").toString());
String tableNames="app_fd_oic_employee_details";
String formDefId = "employee_info";
String fieldID="employee_ID";
rowsFileMetaData.add(row);
formDataDao.saveOrUpdate("employee_info", "oic_employee_details", rowsFileMetaData);
//Set user details
UserSecurity us = DirectoryUtil.getUserSecurity();
RoleDao roleDao = (RoleDao) AppUtil.getApplicationContext().getBean("roleDao");
UserDao userDao = (UserDao) AppUtil.getApplicationContext().getBean("userDao");
FormDataDao formDatadao = (FormDataDao) AppUtil.getApplicationContext().getBean("formDataDao");
DepartmentDao departmentDao = (DepartmentDao) AppUtil.getApplicationContext().getBean("departmentDao");
EmploymentDao employmentDao = (EmploymentDao) AppUtil.getApplicationContext().getBean("employmentDao");
OrganizationDao organizationDao = (OrganizationDao) AppUtil.getApplicationContext().getBean("organizationDao");
// Collection<Organization> organizations = organizationDao.getOrganizationsByFilter(null, object.get("COMPANY_NAME").toString(), false, null, null);
// model.addAttribute("organizations", organizations);
// model.addAttribute("roles", roleDao.getRoles(null, object.get("COMPANY_NAME").toString(), false, null, null));
// model.addAttribute("timezones", TimeZoneUtil.getList());
// Map<String, String> status = new HashMap<String, String>();
// status.put("1", "Active");
// status.put("0", "Inactive");
// model.addAttribute("status", status);
// model.addAttribute("user", user);
// model.addAttribute("employeeCode", object.get("EMPLOYEE_NUMBER").toString());
// model.addAttribute("employeeRole", employeeRole);
// model.addAttribute("employeeOrganization", object.get("COMPANY_NAME").toString());
// model.addAttribute("employeeDepartment", object.get("DEPARTMENT").toString());
// model.addAttribute("employeeGrade", employeeGrade);
// model.addAttribute("employeeStartDate", employeeStartDate);
// model.addAttribute("employeeEndDate", employeeEndDate);
// model.addAttribute("employeeDepartmentHod", employeeDepartmentHod);
//AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
{
//Get the submitted data for each grid row
String password = "password";
String username = object.get("EMPLOYEE_NUMBER").toString();
//System.out.println(username + " username");
//Check if there is user security implementation, using it to encrypt password
User userN = userDao.getUser(username);
//System.out.println(username + "******************** userName** " + userN );
if(userN!=null)
{
if (us != null)
{
userN.setPassword(us.encryptPassword(username, password));
} else {
userN.setPassword(StringUtil.md5Base16(password));
}
userN.setConfirmPassword(password);
userDao.updateUser(userN);
}
else{
User user = new User();
user.setId(username);
user.setUsername(username);
user.setActive(1);
user.setFirstName(object.get("FIRST_NAME").toString());
user.setLastName(object.get("LAST_NAME").toString());
user.setEmail(object.get("EMAIL_ADDRESS").toString());
//Check if there is user security implementation, using it to encrypt password
if (us != null) {
System.out.println("1.********************************************: "+password);
user.setPassword(us.encryptPassword(username, password));
} else {
System.out.println("2.********************************************: "+password);
user.setPassword(StringUtil.md5Base16(password));
}
user.setConfirmPassword(password);
//set user role
Set roleSet = new HashSet();
roleSet.add(roleDao.getRole("ROLE_USER"));
user.setRoles(roleSet);
userDao.addUser(user);
if (us != null) {
us.insertUserPostProcessing(user);
}
}
//End Here User Details
}
//End For loop **********
}
System.out.println("***Record has been created successfully***");
}//End Main bracket json parser