gt:             int start = Integer.parseInt(request.getParameter("start"));
context: /*
   * Copyright (C) 2012  Krawler Information Systems Pvt Ltd
   * All rights reserved.
   * 
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   * 
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
  package com.krawler.esp.handlers;
  
  import com.krawler.common.admin.AuditAction;
  import com.krawler.common.admin.AuditGroup;
  import com.krawler.common.admin.AuditTrail;
  import com.krawler.common.admin.Company;
  import com.krawler.common.admin.CompanyHoliday;
  import com.krawler.common.admin.CompanyPreferences;
  import com.krawler.common.admin.Country;
  import com.krawler.common.admin.KWLCurrency;
  import com.krawler.common.admin.KWLDateFormat;
  import com.krawler.common.admin.KWLTimeZone;
  import com.krawler.common.admin.ProjectFeature;
  import com.krawler.common.admin.Role;
  import com.krawler.common.admin.User;
  import com.krawler.common.admin.UserLogin;
  import com.krawler.common.admin.UserPermission;
  import com.krawler.common.admin.Useraccount;
  import com.krawler.common.admin.hrms_Modules;
  import com.krawler.common.locale.MessageSourceProxy;
  import com.krawler.common.service.ServiceException;
  import com.krawler.common.session.SessionExpiredException;
  import com.krawler.common.util.KWLErrorMsgs;
  import com.krawler.common.util.LocaleUtil;
  import com.krawler.common.util.StringUtil;
  import com.krawler.common.util.URLUtil;
  import com.krawler.esp.hibernate.impl.HibernateUtil;
  import com.krawler.esp.servlets.ProfileImageServlet;
  import com.krawler.esp.web.resource.Links;
  import com.krawler.hrms.performance.Assignmanager;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Set;
  import javax.mail.MessagingException;
  import org.apache.commons.fileupload.FileItem;
  import com.krawler.utils.json.base.*;
  import org.hibernate.Session;
  import com.krawler.hrms.ess.Emphistory;
  import com.krawler.hrms.ess.Empprofile;
  import com.krawler.hrms.performance.Assignreviewer;
  import java.util.UUID;
  import masterDB.Payhistory;
  import org.hibernate.HibernateException;
  import com.krawler.esp.Search.SearchBean;
  import org.apache.lucene.search.Hit;
  import org.apache.lucene.search.Hits;
  import org.springframework.web.servlet.support.RequestContextUtils;
  import javax.servlet.http.HttpServletRequest;
  
  public class ProfileHandler {
  
      public static String getUserFullName(Session session, String userid) throws ServiceException {
          String name = null;
          try {
              String SELECT_USER_INFO = "select u.firstName, u.lastName from User as u " +
                      "where u.userID = ?";
              List list = HibernateUtil.executeQuery(session, SELECT_USER_INFO, userid);
              Iterator ite = list.iterator();
              if (ite.hasNext()) {
                  Object[] row = (Object[]) ite.next();
                  name = (StringUtil.isNullOrEmpty((String) row[0]) ? "" : row[0]) + " " + (StringUtil.isNullOrEmpty((String) row[1]) ? "" : row[1]);
              }
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getUserFullName", e);
          }
  
          return name;
      }
  
      public static JSONObject getAllUserDetails(Session session, HttpServletRequest request, int start, int limit) throws ServiceException {
          JSONObject jobj = new JSONObject();
          int count;
          try {
              String companyid = AuthHandler.getCompanyid(request);
  //			String SELECT_USER_INFO = "select userID, userLogin.userName, firstName, lastName, image, " +
  //			"emailID,userLogin.lastActivityDate, aboutUser, address, contactNumber,designationid.value,salary,accno,department.id,designationid.id,role.ID from User where company.companyID=? and deleted=?";
              String SELECT_USER_INFO="from User where company.companyID=? and deleteflag=?";
  			List list = HibernateUtil.executeQuery(session, SELECT_USER_INFO, new Object[]{AuthHandler.getCompanyid(request),0});
              count = list.size();
  			List list1 = HibernateUtil.executeQueryPaging(session, SELECT_USER_INFO, new Object[]{AuthHandler.getCompanyid(request),false}, new Integer[]{start, limit});
              Iterator itr = list1.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  User u=(User) itr.next();
                  Useraccount ua = (Useraccount) session.get(Useraccount.class, u.getUserID());
                  //Object[] row = (Object[]) itr.next();
                  JSONObject obj = new JSONObject();
                  // obj.put("department",(StringUtil.isNullOrEmpty(u.getDepartment().getValue())? "" : u.getDepartment().getValue()));
                  obj.put("department",(ua.getDepartment() == null ? "" : ua.getDepartment().getId()));
                  obj.put("departmentname",(ua.getDepartment() == null ? "" : ua.getDepartment().getValue()));
                  obj.put("role",(ua.getRole() == null ? "" :ua.getRole().getID()));
                  String name="";
                  if(ua.getRole()!=null&&ua.getRole().getCompany()!=null){
                  	name = ua.getRole().getName();
                  }else{
                  	name = MessageSourceProxy.getMessage("hrms.common.role."+ua.getRole().getID() ,null,ua.getRole().getName(), LocaleUtil.getCompanyLocale(AuthHandler.getCompanyid(request),0));
                  }
                  obj.put("rolename",(ua.getRole() == null ? "" :name));
  				obj.put("userid",u.getUserID() );
  				obj.put("username",u.getUserLogin().getUserName());
  				obj.put("fname",u.getFirstName() );
  				obj.put("lname",u.getLastName() );
                  obj.put("image", u.getImage());
  				obj.put("emailid",u.getEmailID());
                  obj.put("lastlogin", (u.getUserLogin().getLastActivityDate() == null ? "" : AuthHandler.getDateFormatter(request).format(u.getUserLogin().getLastActivityDate())));
                  obj.put("aboutuser", u.getAboutUser());
                  obj.put("address", u.getAddress());
                  obj.put("contactno", u.getContactNumber());
  				obj.put("designation",ua.getDesignationid()== null?"":ua.getDesignationid().getValue() );
                  obj.put("designationid",ua.getDesignationid()== null?"":ua.getDesignationid().getId());
                  obj.put("salary",ua.getSalary());
                  obj.put("accno", ua.getAccno());
                 // obj.put("employeeid", ua.getEmployeeid() == null ? "" : hrmsManager.getEmpidFormatEdit(session, request, u.getEmployeeid()));
  //                 obj.put("department",(row[13] == null ? "" : row[13]));
  //                obj.put("role",(row[15] == null ? "" : row[15]));
  //				obj.put("userid", row[0]);
  //				obj.put("username", row[1]);
  //				obj.put("fname", row[2]);
  //				obj.put("lname", row[3]);
  //				obj.put("image", row[4]);
  //				obj.put("emailid", row[5]);
  //				obj.put("lastlogin", (row[6] == null ? "" : AuthHandler.getDateFormatter(request).format(row[6])));
  //				obj.put("aboutuser", row[7]);
  //				obj.put("address", row[8]);
  //				obj.put("contactno", row[9]);
  //				obj.put("designation", row[10]);
  //                obj.put("designationid",row[14]);
  //                obj.put("salary",row[11]);
  //                obj.put("accno", row[12]);
  //				if(!StringUtil.isNullOrEmpty(row[11].toString()))
  //				{
  //					Newuser nw=(Newuser) session.load(Newuser.class,row[11].toString());
  //					obj.put("salary",nw.getSalary());
  //				}
  
                  List lst1 = HibernateUtil.executeQuery(session, "from  Assignmanager where assignemp.userID=? and managerstatus=1", u.getUserID());
                  Iterator itr1 = lst1.iterator();
                  if (itr1.hasNext()) {
                      while (itr1.hasNext()) {
                          Assignmanager asm = (Assignmanager) itr1.next();
                          obj.append("manager", asm.getAssignman().getFirstName() + " " + asm.getAssignman().getLastName());
                      }
                  } else {
                      obj.put("manager", " ");
                  }
                  jArr.put(obj);
              }
              jobj.put("count", count);
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getAllUserDetails", e);
          }
  
          return jobj;
      }
  
      public static JSONObject getValidUserOptions(Session session, String userid) throws ServiceException {
          return new JSONObject();
      }
  
      public static JSONObject saveUser(Session session, HttpServletRequest request, HashMap hm) throws ServiceException {
          JSONObject obj = new JSONObject();
  //        Integer codeid2 = null;
  //        try {
  //            String id = (String) hm.get("userid");
  //             if (StringUtil.isNullOrEmpty((String) hm.get("employeeid")) == false) {
  //            String[] codeid = ((String) hm.get("employeeid")).split("-");
  //
  //            for (int x = 0; x < codeid.length; x++) {
  //                if (codeid[x].matches("[0-9]*") == true) {
  //                    codeid2 = Integer.parseInt(codeid[x]);
  //                }
  //            }
  //             }
  //            UserLogin userLogin;
  //            String auditMessage="";
  //            String auditID="";
  //            User user;
  //            User creater= (User)session.load(User.class, AuthHandler.getUserid(request));
  //            String fullnameCreator = AuthHandler.getFullName(creater);
  //            String companyid=AuthHandler.getCompanyid(request);
  //            String pwd = null;
  ////			if (id != null && id.length() > 0) {
  ////				user = (User) session.load(User.class, id);
  ////				userLogin = user.getUserLogin();
  ////                obj.put("msg", "User has been edited successfully");
  ////			}
  //            if (StringUtil.isNullOrEmpty(id) == false) {
  //                user = (User) session.load(User.class, id);
  //                userLogin = user.getUserLogin();
  //                String pass = (String) hm.get("password");
  //                String oldpass = (String) hm.get("oldpassword");
  //                if (StringUtil.isNullOrEmpty(oldpass) == false) {
  //                    if (AuthHandler.getSHA1(oldpass).equals(userLogin.getPassword())) {
  //                        if (StringUtil.isNullOrEmpty(pass) == false) {
  //                            userLogin.setPassword(AuthHandler.getSHA1(pass));
  //                        }
  //                    } else {
  //                        throw new Exception("Old Password does not match");
  //                    }
  //                }
  //                 String q2 = "from User where employeeid=? and userID=? and company.companyID=?";
  //                 String q3 = "from User where employeeid=? and company.companyID=?";
  //                if (HibernateUtil.executeQuery(session, q2, new Object[]{codeid2,(String)hm.get("userid"),companyid}).isEmpty() == true) {
  //                   if (HibernateUtil.executeQuery(session, q3, new Object[]{codeid2,companyid}).isEmpty() == false) {
  //                    throw new Exception("Employee ID already present");
  //                   }
  //                }
  //                if (user.getUserID().equals(AuthHandler.getUserid(request))) {
  //                    auditMessage = "User " + fullnameCreator + " has modified his profile";
  //                    auditID = AuditAction.PROFILE_CHANGED;
  //                } else {
  //                    auditMessage = "Profile of user " + AuthHandler.getFullName(user) + " has been modified by " + fullnameCreator;
  //                    auditID = AuditAction.USER_MODIFIED;
  //                }
  //                  if (StringUtil.isNullOrEmpty((String) hm.get("templateid")) == false) {
  //                     user.setTemplateid((String) hm.get("templateid"));
  //                }
  //                  else {
  //                     user.setTemplateid(" ");
  //                }
  //                obj.put("msg", "User has been edited successfully");
  //            } else {
  //                String uuid=UUID.randomUUID().toString();
  //                user = new User();
  //                userLogin = new UserLogin();
  //                userLogin.setUserID(uuid);
  //                user.setUserLogin(userLogin);
  //                userLogin.setUser(user);
  //                String q = "from User where userLogin.userName=?";// and company.companyID=?";
  //                if (HibernateUtil.executeQuery(session, q, new Object[]{hm.get("username")}).isEmpty() == false && hm.get("username").equals(userLogin.getUserName()) == false) {
  //                    throw new Exception("User name not available");
  //                }
  //                String q2 = "from User where employeeid=? and company.companyID=?";
  //                if (HibernateUtil.executeQuery(session, q2, new Object[]{codeid2,companyid}).isEmpty() == false) {
  //                    throw new Exception("Employee ID already present");
  //                }
  //
  //
  //
  //                userLogin.setUserName((String) hm.get("username"));
  //                pwd = AuthHandler.generateNewPassword();
  //                userLogin.setPassword(AuthHandler.getSHA1(pwd));
  //                user.setCompany((Company) session.load(Company.class, AuthHandler.getCompanyid(request)));
  //                obj.put("msg", "User has been saved successfully");
  //            }
  //
  //            user.setFirstName((String) hm.get("fname"));
  //            user.setLastName((String) hm.get("lname"));
  //            user.setEmailID((String) hm.get("emailid"));
  //            user.setAddress((String) hm.get("address"));
  //            user.setContactNumber((String) hm.get("contactno"));
  //            user.setEmployeeid(codeid2);
  //            if (StringUtil.isNullOrEmpty((String) hm.get("roleid")) == false) {
  //                user.setRole((Role) session.load(Role.class, (String) hm.get("roleid")));
  //            }
  //            if (StringUtil.isNullOrEmpty((String) hm.get("designationid")) == false) {
  //                user.setDesignation("none");
  //                user.setDesignationid((MasterData) session.load(MasterData.class, (String) hm.get("designationid")));
  //            }
  //            if (StringUtil.isNullOrEmpty((String) hm.get("department")) == false) {
  //                user.setDepartment((MasterData) session.load(MasterData.class, (String) hm.get("department")));
  //            }
  //            if (StringUtil.isNullOrEmpty((String) hm.get("salary")) == false) {
  //                if (((String) hm.get("salary")).length() > 0) {
  //                    user.setSalary((String) hm.get("salary"));
  //                } else {
  //                    user.setSalary("0");
  //                }
  //            }
  //
  //            if (StringUtil.isNullOrEmpty((String) hm.get("accno")) == false) {
  //                if (((String) hm.get("accno")).length() > 0) {
  //                    user.setAccno((String) hm.get("accno"));
  //                } else {
  //                    user.setAccno("0");
  //                }
  //            }
  //
  //            if (StringUtil.isNullOrEmpty((String) hm.get("formatid")) == false) {
  //                user.setDateFormat((KWLDateFormat) session.load(KWLDateFormat.class, (String) hm.get("formatid")));
  //            }
  //            String diff = null;
  //            if (StringUtil.isNullOrEmpty((String) hm.get("tzid")) == false) {
  //                KWLTimeZone timeZone = (KWLTimeZone) session.load(KWLTimeZone.class, (String) hm.get("tzid"));
  //                diff = timeZone.getDifference();
  //                user.setTimeZone(timeZone);
  //            }
  //            if (StringUtil.isNullOrEmpty((String) hm.get("aboutuser")) == false) {
  //                user.setAboutUser((String) hm.get("aboutuser"));
  //            }
  //
  //            String imageName = ((FileItem) (hm.get("userimage"))).getName();
  //            if (StringUtil.isNullOrEmpty(imageName) == false) {
  //                session.saveOrUpdate(user);
  //                String fileName = user.getUserID() + FileUploadHandler.getImageExt();
  //                user.setImage(ProfileImageServlet.ImgBasePath + fileName);
  //                new FileUploadHandler().uploadImage((FileItem) hm.get("userimage"),
  //                        fileName,
  //                        StorageHandler.GetProfileImgStorePath(), 100, 100, false, false);
  //            }
  //            //  user.setCompany((Company)session.load(Company.class,AuthHandler.getCompanyid(request)));
  //            session.saveOrUpdate(userLogin);
  //            session.saveOrUpdate(user);
  //            SessionHandler.updatePreferences(request, null, (StringUtil.isNullOrEmpty((String) hm.get("formatid")) ? null : (String) hm.get("formatid")), (StringUtil.isNullOrEmpty((String) hm.get("tzid")) ? null : (String) hm.get("tzid")), diff);
  //            if (StringUtil.isNullOrEmpty(id)) {
  ////                String permission="from ProjectFeature where displayfeaturename=?";
  ////                List perm=HibernateUtil.executeQuery(session, permission,"Employee");
  ////                ProjectFeature perms=(ProjectFeature) perm.get(0);
  ////
  ////                ProjectFeature perms1=(ProjectFeature) session.load(ProjectFeature.class,perms.getFeatureID());
  ////                UserPermission uperm=new UserPermission();
  ////                uperm.setFeature(perms1);
  ////                uperm.setRole(userLogin);
  ////                uperm.setPermissionCode(1);
  ////                session.save(uperm);
  //
  //
  //                String uri = URLUtil.getPageURL(request, Links.loginpageFull);
  //                String pmsg = String.format(KWLErrorMsgs.msgMailInvite, user.getFirstName(), fullnameCreator, userLogin.getUserName(), pwd, uri, fullnameCreator);
  //                String htmlmsg = String.format(KWLErrorMsgs.msgMailInviteUsernamePassword, user.getFirstName(), fullnameCreator, AuthHandler.getCompanyName(request), userLogin.getUserName(),
  //                        pwd, uri, uri, fullnameCreator);
  ////				try {
  ////					// SendMailHandler.postMail(new String[] { user.getEmailID() },"[Deskera] Welcome to Deskera HRMS", htmlmsg, pmsg, creater.getEmailID());
  ////				} catch (ConfigurationException e) {
  ////					e.printStackTrace();
  ////				} catch (MessagingException e) {
  ////					e.printStackTrace();
  ////			}
  //            }
  //        } catch (Exception e) {
  //            e.printStackTrace();
  //            throw ServiceException.FAILURE(e.getMessage(), e);
  //        }
          return obj;
      }
  
      public static void deleteUser(Session session, HttpServletRequest request) throws ServiceException {
          try {
              JSONObject jobj = new JSONObject();
              JSONArray jArr = new JSONArray();
  
              String[] ids = request.getParameterValues("userids");
              for (int i = 0; i < ids.length; i++) {
                  User u = (User) session.load(User.class, ids[i]);
                  if (u.getRoleID() == 1) {
                      throw new Exception("Cannot delete Administrator");
                  }
                  //UserLogin userLogin = (UserLogin) session.load(UserLogin.class, ids[i]);
                  u.setDeleteflag(1);
                  session.saveOrUpdate(u);
              //session.delete(userLogin);
              }
          } catch (Exception e) {
              throw ServiceException.FAILURE(e.getMessage(), e);
          }
      }
  
      public static void setPassword(Session session, HttpServletRequest request) throws ServiceException {
          try {
              String password = request.getParameter("password");
              if (password == null || password.length() <= 0) {
                  password = AuthHandler.generateNewPassword();
              }
              String newpass = AuthHandler.getSHA1(password);
              User user = (User) session.load(User.class, request.getParameter("userid"));
              UserLogin userLogin = user.getUserLogin();
              userLogin.setPassword(newpass);
              session.saveOrUpdate(userLogin);
              String uri = URLUtil.getPageURL(request, Links.loginpageFull);
              String fname = user.getFirstName();
              if (StringUtil.isNullOrEmpty(fname)) {
                  fname = user.getUserLogin().getUserName();
              }
              String pmsg = String.format(
                      KWLErrorMsgs.msgTempPassword,
                      fname, password, uri);
              String htmlmsg = String.format(
                      KWLErrorMsgs.msgMailPassword,
                      fname, password, uri, uri);
              try {
                  String adminEmailId = request.getSession().getAttribute("sysemailid").toString();
                  SendMailHandler.postMail(new String[]{user.getEmailID()},
                          KWLErrorMsgs.msgMailSubjectPassword, htmlmsg,
                          pmsg, adminEmailId);
              } catch (MessagingException e) {
                  e.printStackTrace();
              }
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.setPassword", e);
          }
      }
  
      public static void setPermissions(Session session, HttpServletRequest request) throws ServiceException {
          try {
              String id = request.getParameter("roleid");
              String[] features = request.getParameterValues("features");
              String[] permissions = request.getParameterValues("permissions");
              String sql = "delete from UserPermission where role.ID=?";
              HibernateUtil.executeUpdate(session, sql, id);
              Role role = (Role) session.load(Role.class, id);
              for (int i = 0; i < features.length; i++) {
                  if (permissions[i].equals("0")) {
                      continue;
                  }
                  UserPermission permission = new UserPermission();
                  permission.setRole(role);
                  permission.setFeature((ProjectFeature) session.load(ProjectFeature.class, features[i]));
                  permission.setPermissionCode(Long.parseLong(permissions[i]));
                  session.save(permission);
              }
             // insertAuditLog(session, AuditAction.PERMISSIONS_MODIFIED, AuthHandler.getFullName(session, AuthHandler.getUserid(request)) + " has changed the permissions of role " + role.getName(), request);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.setPermissions", e);
          }
      }
  
      public static void updateLastLogin(Session session, HttpServletRequest request) throws ServiceException {
          try {
              UserLogin userLogin = (UserLogin) session.load(UserLogin.class, AuthHandler.getUserid(request));
              userLogin.setLastActivityDate(new Date());
              session.update(userLogin);
           //   insertAuditLog(session, AuditAction.LOG_IN_SUCCESS, "User " + AuthHandler.getFullName(session, AuthHandler.getUserid(request)) + " has logged in", request);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.updateLastLogin", e);
          }
      }
  
      public static JSONObject getAllTimeZones(Session session, HttpServletRequest request) throws ServiceException {
          JSONObject jobj = new JSONObject();
          try {
              String query = "from KWLTimeZone";
              List list = HibernateUtil.executeQuery(session, query);
              Iterator itr = list.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  KWLTimeZone timeZone = (KWLTimeZone) itr.next();
                  JSONObject obj = new JSONObject();
                  obj.put("id", timeZone.getTimeZoneID());
                  obj.put("name", timeZone.getName());
                  obj.put("difference", timeZone.getDifference());
                  jArr.put(obj);
              }
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getAllTimeZones", e);
          }
  
          return jobj;
      }
  
      public static JSONObject getAllCurrencies(Session session, HttpServletRequest request) throws ServiceException {
          JSONObject jobj = new JSONObject();
          try {
              String query = "from KWLCurrency";
              List list = HibernateUtil.executeQuery(session, query);
              Iterator itr = list.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  KWLCurrency currency = (KWLCurrency) itr.next();
                  JSONObject obj = new JSONObject();
                  obj.put("currencyid", currency.getCurrencyID());
                  obj.put("symbol", currency.getSymbol());
                  obj.put("currencyname", currency.getName());
                  obj.put("htmlcode", currency.getHtmlcode());
                  jArr.put(obj);
              }
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getAllCurrencies", e);
          }
  
          return jobj;
      }
  
      public static JSONObject getCompanyInformation(Session session, HttpServletRequest request) throws ServiceException {
          JSONObject jobj = new JSONObject();
          JSONObject modobj=new JSONObject();
          try {
              String query = "select c,cpr from CompanyPreferences c right outer join c.company cpr where cpr.companyID=?";
              List list = HibernateUtil.executeQuery(session, query, AuthHandler.getCompanyid(request));
              Iterator itr = list.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  Object[] row = (Object[]) itr.next();
                  Company company =(Company) row[1];
                  CompanyPreferences cmpr=null;
                  JSONObject obj = new JSONObject();
                  obj.put("phone", company.getPhoneNumber());
                  obj.put("state", company.getState());
                  obj.put("currency", (company.getCurrency() == null ? "1" : company.getCurrency().getCurrencyID()));
                  obj.put("city", company.getCity());
                  obj.put("emailid", company.getEmailID());
                  obj.put("companyid", company.getCompanyID());
                  obj.put("timezone", (company.getTimeZone() == null ? "1" : company.getTimeZone().getTimeZoneID()));
                  obj.put("zip", company.getZipCode());
                  obj.put("fax", company.getFaxNumber());
                  obj.put("website", company.getWebsite());
                  obj.put("image", company.getCompanyLogo());
                  obj.put("modifiedon", (company.getModifiedOn() == null ? "" : AuthHandler.getDateFormatter(request).format(company.getModifiedOn())));
                  obj.put("createdon", (company.getCreatedOn() == null ? "" : AuthHandler.getDateFormatter(request).format(company.getCreatedOn())));
                  obj.put("companyname", company.getCompanyName());
                  obj.put("country", (company.getCountry() == null ? "" : company.getCountry().getID()));
                  obj.put("address", company.getAddress());
                  obj.put("subdomain", company.getSubDomain());
                  //obj.put("subscription",company.getSubscriptionCode());
                  if(row[0]!=null){
                  cmpr=(CompanyPreferences)row[0];
                  obj.put("employeeidformat", cmpr.getEmpidformat());
                  obj.put("jobidformat", cmpr.getJobidformat());
                  obj.put("selfapp",cmpr.isSelfappraisal());
                  obj.put("competency",cmpr.isCompetency());
                  obj.put("goal",cmpr.isGoal());
                  obj.put("annmng",cmpr.isAnnmanager());
                  obj.put("approveappraisal",cmpr.isApproveappraisal());
                  obj.put("promotionrec",cmpr.isPromotion());
                  obj.put("weightage",cmpr.isWeightage());
                  obj.put("reviewappraisal",cmpr.isReviewappraisal());
                  obj.put("partial",cmpr.isPartial());
                  obj.put("fullupdates",cmpr.isFullupdates());
                  obj.put("modaverage",cmpr.isModaverage());
                  }else{
                  obj.put("selfapp",false);
                  obj.put("competency",false);
                  obj.put("goal",false);
                  obj.put("annmng",false);
                  obj.put("approveappraisal",false);
                  obj.put("promotionrec",false);
                  obj.put("weightage",false);
                  obj.put("reviewappraisal",false);
                  obj.put("partial",false);
                  obj.put("fullupdates",false);
                  obj.put("modaverage",false);
                  }
                  modobj=gethrmsModules(session);
                  obj.put("modules",modobj);
                  jArr.put(obj);
              }
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getCompanyInformation", e);
          }
  
          return jobj;
      }
  
      public static JSONObject getCompanyHolidays(Session session, HttpServletRequest request) throws ServiceException {
          JSONObject jobj = new JSONObject();
          try {
              String query = "from CompanyHoliday where company.companyID=? order by holidayDate";
              List list = HibernateUtil.executeQuery(session, query, AuthHandler.getCompanyid(request));
              Iterator itr = list.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  CompanyHoliday holiday = (CompanyHoliday) itr.next();
                  JSONObject obj = new JSONObject();
                  obj.put("holiday", AuthHandler.getDateFormatter(request).format(holiday.getHolidayDate()));
                  obj.put("description", holiday.getDescription());
                  jArr.put(obj);
              }
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getCompanyHolidays", e);
          }
  
          return jobj;
      }
  
      public static JSONObject getAllCountries(Session session, HttpServletRequest request) throws ServiceException {
          JSONObject jobj = new JSONObject();
          try {
              String query = "from Country";
              List list = HibernateUtil.executeQuery(session, query);
              Iterator itr = list.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  Country country = (Country) itr.next();
                  JSONObject obj = new JSONObject();
                  obj.put("id", country.getID());
                  obj.put("name", country.getCountryName());
                  jArr.put(obj);
              }
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getAllCountries", e);
          }
  
          return jobj;
      }
  
      public static void updateCompany(Session session, HttpServletRequest request, HashMap hm) throws ServiceException {
          try {
              CompanyPreferences cp = (CompanyPreferences) session.load(CompanyPreferences.class, AuthHandler.getCompanyid(request));
              Company company = cp.getCompany();
              company.setCompanyName((String) hm.get("companyname"));
              company.setAddress((String) hm.get("address"));
              company.setCity((String) hm.get("city"));
              company.setState((String) hm.get("state"));
              company.setZipCode((String) hm.get("zip"));
              company.setPhoneNumber((String) hm.get("phone"));
              company.setFaxNumber((String) hm.get("fax"));
              company.setWebsite((String) hm.get("website"));
              company.setEmailID((String) hm.get("mail"));
              company.setSubDomain((String) hm.get("domainname"));
              cp.setEmpidformat((String) hm.get("employeeidformat"));
              company.setCountry((Country) session.load(Country.class, (String) hm.get("country")));
              company.setCurrency((KWLCurrency) session.load(KWLCurrency.class, (String) hm.get("currency")));
              KWLTimeZone timeZone = (KWLTimeZone) session.load(KWLTimeZone.class, (String) hm.get("timezone"));
              company.setTimeZone(timeZone);
              company.setModifiedOn(new Date());
              JSONArray jArr = new JSONArray((String) hm.get("holidays"));
              Set<CompanyHoliday> holidays = company.getHolidays();
              holidays.clear();
              DateFormat formatter = AuthHandler.getDateFormatter(request);
              for (int i = 0; i < jArr.length(); i++) {
                  CompanyHoliday day = new CompanyHoliday();
                  JSONObject obj = jArr.getJSONObject(i);
                  day.setDescription(obj.getString("description"));
                  day.setHolidayDate(formatter.parse(obj.getString("day")));
                  day.setCompany(company);
                  holidays.add(day);
              }
              String imageName = ((FileItem) (hm.get("logo"))).getName();
              if (StringUtil.isNullOrEmpty(imageName) == false) {
                  String fileName = AuthHandler.getCompanyid(request) + FileUploadHandler.getCompanyImageExt();
                  company.setCompanyLogo(ProfileImageServlet.ImgBasePath + fileName);
                  new FileUploadHandler().uploadImage((FileItem) hm.get("logo"),
                          fileName,
                          StorageHandler.GetProfileImgStorePath(), 130, 25, true, false);
              }
              session.update(company);
              SessionHandler.updatePreferences(request, (String) hm.get("currency"), null, (String) hm.get("timezone"), timeZone.getDifference());
             // insertAuditLog(session, AuditAction.COMPANY_UPDATION, "User " + AuthHandler.getUserName(request) + " changed company details", request);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.updateCompany", e);
          }
      }
  
      public static JSONObject getManagers(Session session, HttpServletRequest request) throws ServiceException {
          JSONObject jobj = new JSONObject();
          try {
              String SELECT_USER_INFO = "from User where (role.ID=? or role.ID=?) and company.companyID=? and deleteflag=? order by firstName";
              List list = HibernateUtil.executeQuery(session, SELECT_USER_INFO, new Object[]{Role.COMPANY_MANAGER,Role.COMPANY_ADMIN, AuthHandler.getCompanyid(request),0});
              Iterator itr = list.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  User u = (User) itr.next();
                  Useraccount ua = (Useraccount) session.get(Useraccount.class, u.getUserID());
                  JSONObject obj = new JSONObject();
                  obj.put("userid", u.getUserID());
                  obj.put("username", u.getFirstName() + " " + (u.getLastName()!=null?u.getLastName():""));
                  obj.put("designation", ua.getDesignationid()!=null?ua.getDesignationid().getValue():"");
                  obj.put("department", ua.getDepartment()!=null?ua.getDepartment().getValue():"");
                  jArr.put(obj);
              }
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getAllUserDetails", e);
          }
  
          return jobj;
      }
  
      public static JSONObject getAllCompanyDetails(Session session, HttpServletRequest request, Integer start, Integer limit) throws ServiceException {
          JSONObject jobj = new JSONObject();
          int count;
          String admincmp = "ff808081227de9f701227de9fb410001";
          try {
              String SelectCompany = "from Company where companyID <> ? and deleted=?";
              List list = HibernateUtil.executeQuery(session, SelectCompany, new Object[]{admincmp, false});
              count = list.size();
              List list1 = HibernateUtil.executeQueryPaging(session, SelectCompany, new Object[]{admincmp, false}, new Integer[]{start, limit});
              Iterator itr = list1.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  Company company = (Company) itr.next();
                  JSONObject obj = new JSONObject();
                  obj.put("companyid", company.getCompanyID());
                  obj.put("companyname", company.getCompanyName());
                  //obj.put("currency",company.getCurrency());
                  //obj.put("timezone",company.getTimeZone());
                  //obj.put("subdomain",company.getSubDomain());
                  obj.put("address", company.getAddress());
                  obj.put("city", company.getCity());
                  obj.put("phoneno", company.getPhoneNumber());
                  String uq = "from User where role.ID=1 and company.companyID=?";
                  List cmpls=HibernateUtil.executeQuery(session, uq, company.getCompanyID());
                  try{
                  User u = (User) cmpls.get(0);
                  obj.put("admin_fname", u.getFirstName());
                  obj.put("admin_lname", u.getLastName());
                  obj.put("admin_uname", u.getUserLogin().getUserName());
                  obj.put("emailid", company.getEmailID());
                  //    obj.put("website",company.getWebsite());
                  //obj.put("emailid", company.getEmailID());
                  //	 obj.put("country",(company.getCountry()==null)?"":company.getCountry());//==null?
                  }catch(Exception e){}
                  jArr.put(obj);
              }
              jobj.put("count", count);
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getAllCompanyDetails", e);
          }
          return jobj;
      }
  
      public static JSONObject getUserofCompany(Session session, HttpServletRequest request, Integer start, Integer limit) throws ServiceException {
          JSONObject jobj = new JSONObject();
          int count;
          try {
              String SELECT_USER = "from User as u where u.company.companyID=?";
              String cid = request.getParameter("companyid");
              List list = HibernateUtil.executeQuery(session, SELECT_USER, cid);
              count = list.size();
              List list1 = HibernateUtil.executeQueryPaging(session, SELECT_USER, new Object[]{cid}, new Integer[]{start, limit});
              Iterator itr = list1.iterator();
              JSONArray jArr = new JSONArray();
              while (itr.hasNext()) {
                  User user = (User) itr.next();
                  JSONObject obj = new JSONObject();
                  obj.put("userid", user.getUserID());
                  obj.put("username", user.getFirstName());
                  obj.put("fname", user.getFirstName());
                  obj.put("lname", user.getLastName());
                  obj.put("image", user.getImage());
                  obj.put("emailid", user.getEmailID());
                  obj.put("lastlogin", (user.getUserLogin().getLastActivityDate() == null ? "" : AuthHandler.getDateFormatter(request).format(user.getUserLogin().getLastActivityDate())));
                  obj.put("aboutuser", user.getAboutUser());
                  obj.put("address", user.getAddress());
                  obj.put("contactno", user.getContactNumber());
                  jArr.put(obj);
              }
              jobj.put("count", count);
              jobj.put("data", jArr);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.getAllUserDetails", e);
          }
          return jobj;
      }
  
      public static void deletecompany(Session session, HttpServletRequest request) throws ServiceException {
          try {            
              String[] ids = request.getParameterValues("cmpid");
              for (int i = 0; i < ids.length; i++) {
                  Company c = (Company) session.load(Company.class, ids[i]);
                  c.setDeleted(1);
                  session.saveOrUpdate(c);
              }
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.deletecompanies", e);
          }
      }
  
      public static JSONObject changePassword(String platformURL,Session session, HttpServletRequest request) throws ServiceException {
          JSONObject jobj = new JSONObject();
          String msg = "";
          try {
              String password = request.getParameter("currentpassword");
              String pwd = request.getParameter("changepassword").toString();
              String uid = AuthHandler.getUserid(request);
              String companyid = AuthHandler.getCompanyid(request);
  
              if (password == null || password.length() <= 0) {
                  msg = MessageSourceProxy.getMessage("hrms.common.InvalidPassword", null,"Invalid Password.", RequestContextUtils.getLocale(request));
              } else {
                  //String newpass=AuthHandler.getSHA1(password);
                  if(StringUtil.isStandAlone()){
                      platformURL ="";
                  }
                  if (!StringUtil.isNullOrEmpty(platformURL)) {
                      JSONObject userData = new JSONObject();
                      userData.put("pwd", pwd);
                      userData.put("oldpwd", password);
                      userData.put("userid", uid);
                      userData.put("remoteapikey", StorageHandler.GetRemoteAPIKey());
                      String action = "3";
                      JSONObject resObj = APICallHandler.callApp(platformURL, userData, companyid, action);
                      if (!resObj.isNull("success") && resObj.getBoolean("success")) {
                          User user = (User) session.load(User.class, uid);
                          UserLogin userLogin = user.getUserLogin();
                          userLogin.setPassword(pwd);
                          session.saveOrUpdate(userLogin);
                          msg = MessageSourceProxy.getMessage("hrms.common.PasswordChangedsuccessfully", null,"Invalid Password.", RequestContextUtils.getLocale(request));
  
                      } else {
                          if (!resObj.isNull("errorcode") && resObj.getString("errorcode").equals("e10")) {
                              msg =MessageSourceProxy.getMessage("hrms.common.OldpasswordisincorrectPleasetryagain", null,"Invalid Password.", RequestContextUtils.getLocale(request));
                          } else {
                              msg =MessageSourceProxy.getMessage("hrms.common.ErrorinchangingPassword", null,"Invalid Password.", RequestContextUtils.getLocale(request));
                          }
                      }
                  } else {
                      User user = (User) session.load(User.class, uid);
                      UserLogin userLogin = user.getUserLogin();
                      String currentpass = userLogin.getPassword();
                      if (StringUtil.equal(password, currentpass)) {
                          userLogin.setPassword(pwd);
                          session.saveOrUpdate(userLogin);
                          msg = MessageSourceProxy.getMessage("hrms.common.PasswordChangedsuccessfully", null,"Invalid Password.", RequestContextUtils.getLocale(request));
                      } else {
                          msg =MessageSourceProxy.getMessage("hrms.common.OldpasswordisincorrectPleasetryagain", null,"Invalid Password.", RequestContextUtils.getLocale(request));
                      }
                  }
              }
              jobj.put("msg", msg);
          } catch (Exception e) {
              throw ServiceException.FAILURE("ProfileHandler.setPassword", e);
          }
          return jobj;
      }
  
      public static void insertAuditLog(Session session, String actionid, String details, HttpServletRequest request, String recid) throws ServiceException {
          try {
              AuditAction action = (AuditAction) session.load(AuditAction.class, actionid);
              insertAuditLog(session, action, details, request, recid);
          } catch (Exception e) {
              throw ServiceException.FAILURE(e.getMessage(), e);
          }
      }
  
      public static void insertAuditLog(Session session, String actionid, String details, String ipAddress, String userid, String recid) throws ServiceException {
          try {
              AuditAction action = (AuditAction) session.load(AuditAction.class, actionid);
              User user = (User) session.load(User.class, userid);
              insertAuditLog(session, action, details, ipAddress, user, recid);
          } catch (Exception e) {
              throw ServiceException.FAILURE(e.getMessage(), e);
          }
      }
  
      public static void insertAuditLog(Session session, AuditAction action, String details, HttpServletRequest request, String recid) throws ServiceException {
          try {
              User user = (User) session.load(User.class, AuthHandler.getUserid(request));
              String ipaddr = null;
              if (StringUtil.isNullOrEmpty(request.getHeader("x-real-ip"))) {
                  ipaddr = request.getRemoteAddr();
              } else {
                  ipaddr = request.getHeader("x-real-ip");
              }
  
              insertAuditLog(session, action, details, ipaddr, user, recid);
          } catch (Exception e) {
              throw ServiceException.FAILURE(e.getMessage(), e);
          }
      }
  
      public static void insertAuditLog(Session session, AuditAction action, String details, String ipAddress, User user, String recid) throws ServiceException {
          try {
              String aid = UUID.randomUUID().toString();
              AuditTrail auditTrail = new AuditTrail();
              auditTrail.setID(aid);
              auditTrail.setAction(action);
              auditTrail.setAuditTime(new Date());
              auditTrail.setDetails(details);
              auditTrail.setIPAddress(ipAddress);
              auditTrail.setRecid(recid);
              auditTrail.setExtraid("0");
              auditTrail.setUser(user);
              session.save(auditTrail);
              String id=auditTrail.getID();
  
              ArrayList<Object> indexFieldDetails = new ArrayList<Object>();
                ArrayList<String> indexFieldName = new ArrayList<String>();
                indexFieldDetails.add(details);
                 indexFieldName.add("details");
                 indexFieldDetails.add(id);
                 indexFieldName.add("transactionid");
                 indexFieldDetails.add(action.getID());
                 indexFieldName.add("actionid");
                 indexFieldDetails.add(ipAddress);
                 indexFieldName.add("ipaddr");
                 String userName = user.getUserLogin().getUserName()+" "+user.getFirstName()+" "+user.getLastName();
                 indexFieldDetails.add(userName);
                 indexFieldName.add("username");
                 indexFieldDetails.add(auditTrail.getAuditTime());
                 indexFieldName.add("timestamp");
                  String indexPath = com.krawler.esp.handlers.StorageHandler.GetAuditTrailIndexPath();
                  com.krawler.esp.indexer.KrawlerIndexCreator kwlIndex = new com.krawler.esp.indexer.KrawlerIndexCreator();
                  kwlIndex.setIndexPath(indexPath);
                  com.krawler.esp.indexer.CreateIndex  cIndex = new com.krawler.esp.indexer.CreateIndex();
                  cIndex.indexAlert(kwlIndex, indexFieldDetails, indexFieldName);
          } catch (Exception e) {
              throw ServiceException.FAILURE(e.getMessage(), e);
          }
      }
  
      public static JSONObject getAuditTrail(Session session, HttpServletRequest request) throws ServiceException {
          JSONObject jobj = new JSONObject();
          try {
  