@Override
  public KwlReturnObject saveAccountProject(JSONObject jobj1) throws ServiceException {
    JSONObject jobj = new JSONObject();
    List ll = new ArrayList();
    try {
      String companyid = jobj1.getString("companyid");
      String userid = jobj1.getString("userid");
      String projectName = jobj1.getString("projectName");
      String accId = jobj1.getString("accId");
      jobj.put("projectname", projectName);
      jobj.put("companyid", companyid);
      jobj.put("userid", userid);
      jobj.put("remoteapikey", ConfigReader.getinstance().get("remoteapikey"));

      jobj = apiCallHandlerService.callApp(jobj1.getString("appURL"), jobj, companyid, "12", true);
      if (jobj.has("success") && jobj.getBoolean("success")) {
        jobj = jobj.getJSONObject("data");
        String projectId = jobj.getString("projectid");
        String nickName = jobj.getString("nickname");

        AccountProject ap = new AccountProject();

        if (!StringUtil.isNullOrEmpty(accId)) {
          ap.setAccountId((CrmAccount) get(CrmAccount.class, accId));
        }
        if (!StringUtil.isNullOrEmpty(nickName)) {
          ap.setNickName(nickName);
        }
        if (!StringUtil.isNullOrEmpty(projectId)) {
          ap.setProjectId(projectId);
        }
        if (!StringUtil.isNullOrEmpty(projectName)) {
          ap.setProjectName(projectName);
        }

        save(ap);
        jobj = new JSONObject();
        jobj.put("projectid", ap.getProjectId());
      } else {
        String errorCode = jobj.getString("errorcode");
        jobj = new JSONObject();
        jobj.put("errorcode", errorCode);
      }
      ll.add(jobj);
    } catch (JSONException e) {
      logger.warn(e.getMessage(), e);
      throw ServiceException.FAILURE("crmAccountDAOImpl.saveAccountProject : " + e.getMessage(), e);
    } catch (HibernateException e) {
      logger.warn(e.getMessage(), e);
      throw ServiceException.FAILURE("crmAccountDAOImpl.saveAccountProject : " + e.getMessage(), e);
    } catch (Exception e) {
      logger.warn(e.getMessage(), e);
      throw ServiceException.FAILURE("crmAccountDAOImpl.saveAccountProject : " + e.getMessage(), e);
    }
    return new KwlReturnObject(true, KWLErrorMsgs.S01, "", ll, 0);
  }
 public void setCustomData(CrmAccount crmAcc, JSONArray cstmData) {
   StringBuffer fields = new StringBuffer("accountid,company");
   StringBuffer qmarks = new StringBuffer("?,?");
   ArrayList params = new ArrayList();
   params.add(crmAcc.getAccountid());
   params.add(crmAcc.getCompany().getCompanyID());
   boolean hasValue = false;
   try {
     for (int i = 0; i < cstmData.length(); i++) {
       JSONObject jobj = cstmData.getJSONObject(i);
       if (jobj.has(Constants.Crm_custom_field)) {
         String fieldname = jobj.getString(Constants.Crm_custom_field);
         String fielddbname = jobj.getString(fieldname);
         String fieldValue = jobj.getString(fielddbname);
         hasValue = true;
         fielddbname = fielddbname.replace("c", "C");
         Integer xtype = Integer.parseInt(jobj.getString("xtype"));
         if (!StringUtil.isNullOrEmpty(fieldValue)
             && !StringUtil.isNullOrEmpty(fieldValue.trim())
             && !fieldValue.equalsIgnoreCase(Constants.field_data_undefined)) {
           fields.append(',').append(fielddbname);
           qmarks.append(",?");
           params.add(fieldValue);
         } else {
           if (xtype == 7 || xtype == 8 || xtype == 4) {
             fields.append(',').append(fielddbname);
             qmarks.append(",?");
             params.add(null);
           } else {
             fields.append(',').append(fielddbname);
             qmarks.append(",?");
             params.add("");
           }
         }
       }
     }
   } catch (NumberFormatException e) {
     e.printStackTrace();
   } catch (JSONException e) {
     e.printStackTrace();
   }
   if (hasValue) {
     StringBuffer sql =
         new StringBuffer("insert into crmaccountcustomdata (")
             .append(fields)
             .append(")VALUES(")
             .append(qmarks)
             .append(')');
     updateJDBC(sql.toString(), params.toArray());
   }
 }
  public static JSONArray getProductTypesJson(HttpServletRequest request, List list)
      throws ServiceException, SessionExpiredException {
    JSONArray jArr = new JSONArray();
    try {
      Iterator itr = list.iterator();
      while (itr.hasNext()) {
        Producttype ptype = (Producttype) itr.next();
        JSONObject obj = new JSONObject();
        obj.put("id", ptype.getID());
        obj.put("name", ptype.getName());
        jArr.put(obj);
      }

    } catch (JSONException ex) {
      throw ServiceException.FAILURE("getProductTypesJson : " + ex.getMessage(), ex);
    }
    return jArr;
  }
  @Override
  public KwlReturnObject updateMassAccount(JSONObject jobj) throws ServiceException {
    List ll = new ArrayList();
    String hqlVarPart = "";
    List<Object> params = new ArrayList<Object>();
    int dl = 0;
    try {
      String companyid = null;
      String userid = null;
      String[] accountids = (String[]) jobj.get("accountid");

      if (jobj.has("accountname")) {
        hqlVarPart += " accountname = ?,";
        params.add(jobj.getString("accountname"));
      }
      if (jobj.has("accounttypeid")) {
        hqlVarPart += " crmCombodataByAccounttypeid = ?,";
        params.add(get(DefaultMasterItem.class, jobj.getString("accounttypeid")));
      }
      if (jobj.has("companyid")) {
        hqlVarPart += " company = ?,";
        params.add(get(Company.class, jobj.getString("companyid")));
      }
      if (jobj.has("description")) {
        hqlVarPart += " description = ?,";
        params.add(jobj.getString("description"));
      }
      if (jobj.has("website")) {
        hqlVarPart += " website = ?,";
        params.add(jobj.getString("website"));
      }
      if (jobj.has("email")) {
        hqlVarPart += " email = ?,";
        params.add(jobj.getString("email"));
      }
      if (jobj.has("address")) {
        hqlVarPart += " mailstreet = ?,";
        params.add(jobj.getString("address"));
      }
      if (jobj.has("productid")) {
        saveAccountProducts(accountids, jobj.getString("productid").split(","));
      }
      if (jobj.has("price")) {
        hqlVarPart += " price = ?,";
        params.add(jobj.getString("price"));
      }
      if (jobj.has("phone")) {
        hqlVarPart += " phone = ?,";
        params.add(jobj.getString("phone"));
      }
      if (jobj.has("revenue")) {
        hqlVarPart += " revenue = ?,";
        params.add(jobj.getString("revenue"));
      }
      if (jobj.has("industryid")) {
        hqlVarPart += " crmCombodataByIndustryid = ?,";
        params.add(get(DefaultMasterItem.class, jobj.getString("industryid")));
      }
      if (jobj.has("updatedon")) {
        hqlVarPart += " updatedOn = ?,";
        params.add(jobj.getLong("updatedon"));
      }
      if (jobj.has("createdon")) {
        Long createdOn = jobj.getLong("createdon");
        hqlVarPart += " createdOn = ?,";
        params.add(createdOn);
      }
      if (jobj.has("userid")) {

        hqlVarPart += " usersByUpdatedbyid = ?,";
        params.add(get(User.class, jobj.getString("userid")));
      }
      if (jobj.has("accountownerid")) {
        setMainAccOwner(accountids, jobj.getString("accountownerid"));
      }
      if (jobj.has("validflag")) {
        hqlVarPart += " validflag = ?,";
        params.add(Integer.parseInt(jobj.getString("validflag")));
      }
      if (jobj.has("deleteflag")) {
        hqlVarPart += " deleteflag = ?,";
        params.add(Integer.parseInt(jobj.getString("deleteflag")));
        String query = "delete from AccountProducts where accountid.accountid in (:accountids)";
        Map map = new HashMap();
        map.put("accountids", accountids);
        executeUpdate(query, null, map);
      }
      if (jobj.has("CrmAccountCustomDataobj") && jobj.getBoolean("CrmAccountCustomDataobj")) {
        linkCustomData();
      }
      if (jobj.has("leadid")) {
        hqlVarPart += " crmLead = ?,";
        params.add(get(CrmLead.class, jobj.getString("leadid")));
      }

      hqlVarPart = hqlVarPart.substring(0, Math.max(0, hqlVarPart.lastIndexOf(',')));
      String hql = "update CrmAccount set " + hqlVarPart + " where accountid in (:accountids)";
      Map map = new HashMap();
      map.put("accountids", accountids);
      executeUpdate(hql, params.toArray(), map);

    } catch (JSONException e) {
      throw ServiceException.FAILURE("crmAccountDAOImpl.updateMassAccount : " + e.getMessage(), e);
    } catch (DataAccessException e) {
      throw ServiceException.FAILURE("crmAccountDAOImpl.updateMassAccount : " + e.getMessage(), e);
    } catch (Exception e) {
      throw ServiceException.FAILURE("crmAccountDAOImpl.updateMassAccount : " + e.getMessage(), e);
    }
    return new KwlReturnObject(true, KWLErrorMsgs.S01, "", ll, dl);
  }
  /* (non-Javadoc)
   * @see com.krawler.spring.crm.accountModule.crmAccountDAO#editAccounts(com.krawler.utils.json.base.JSONObject)
   */
  public KwlReturnObject editAccounts(JSONObject jobj) throws ServiceException {
    List ll = new ArrayList();
    int dl = 0;
    try {
      String companyid = null;
      String userid = null;
      String id = "";
      if (jobj.has("accountid")) {
        id = jobj.getString("accountid");
      }
      CrmAccount crmAccount = (CrmAccount) get(CrmAccount.class, id);
      if (jobj.has("accountname")) {
        crmAccount.setAccountname(jobj.getString("accountname"));
      }
      if (jobj.has("accounttypeid")) {
        crmAccount.setCrmCombodataByAccounttypeid(
            (DefaultMasterItem) get(DefaultMasterItem.class, jobj.getString("accounttypeid")));
      }
      if (jobj.has("companyid")) {
        companyid = jobj.getString("companyid");
        crmAccount.setCompany((Company) get(Company.class, companyid));
      }
      if (jobj.has("description")) {
        crmAccount.setDescription(jobj.getString("description"));
      }
      if (jobj.has("website")) {
        crmAccount.setWebsite(jobj.getString("website"));
      }
      if (jobj.has("email")) {
        crmAccount.setEmail(jobj.getString("email"));
      }
      if (jobj.has("address")) {
        crmAccount.setMailstreet(jobj.getString("address"));
      }
      if (jobj.has("productid")) {
        saveAccountProducts(new String[] {id}, jobj.getString("productid").split(","));
      }
      if (jobj.has("price")) {
        crmAccount.setPrice(jobj.getString("price"));
      }
      if (jobj.has("phone")) {
        crmAccount.setPhone(jobj.getString("phone"));
      }
      if (jobj.has("revenue")) {
        crmAccount.setRevenue(jobj.getString("revenue"));
      }
      if (jobj.has("industryid")) {
        crmAccount.setCrmCombodataByIndustryid(
            (DefaultMasterItem) get(DefaultMasterItem.class, jobj.getString("industryid")));
      }
      if (jobj.has("updatedon")) {
        crmAccount.setUpdatedOn(System.currentTimeMillis());
      }
      if (jobj.has("createdon")) {
        crmAccount.setCreatedOn(jobj.getLong("createdon"));
      } else {
        crmAccount.setCreatedOn(System.currentTimeMillis());
      }
      if (jobj.has("userid")) {
        userid = jobj.getString("userid");
        crmAccount.setUsersByUpdatedbyid((User) get(User.class, userid));
        //                crmAccount.setUsersByCreatedbyid((User) hibernateTemplate.get(User.class,
        // userid));
      }
      if (jobj.has("accountownerid")) {
        setMainAccOwner(new String[] {id}, jobj.getString("accountownerid"));
        //                crmAccount.setUsersByUserid((User) hibernateTemplate.get(User.class,
        // jobj.getString("accountownerid")));
      }
      if (jobj.has("validflag")) {
        crmAccount.setValidflag(Integer.parseInt(jobj.getString("validflag")));
      }
      if (jobj.has("isarchive")) {
        crmAccount.setIsarchive(jobj.getBoolean("isarchive"));
      }
      if (jobj.has("deleteflag")) {
        crmAccount.setDeleteflag(Integer.parseInt(jobj.getString("deleteflag")));
        try {
          if (Integer.parseInt(jobj.getString("deleteflag")) == 1) {
            String query = "delete from AccountProducts where accountid.accountid=?";
            executeUpdate(query, id);
          }
        } catch (Exception te) {
          logger.warn("Products related to the account could not be deleted :", te);
        }
      }
      if (jobj.has("CrmAccountCustomDataobj")) {
        crmAccount.setCrmAccountCustomDataobj(
            (CrmAccountCustomData)
                get(CrmAccountCustomData.class, jobj.getString("CrmAccountCustomDataobj")));
      }
      if (jobj.has("leadid")) {
        crmAccount.setCrmLead((CrmLead) get(CrmLead.class, jobj.getString("leadid")));
      }
      saveOrUpdate(crmAccount);

      ll.add(crmAccount);
    } catch (JSONException e) {
      throw ServiceException.FAILURE("crmAccountDAOImpl.editAccounts : " + e.getMessage(), e);
    } catch (DataAccessException e) {
      throw ServiceException.FAILURE("crmAccountDAOImpl.editAccounts : " + e.getMessage(), e);
    } catch (Exception e) {
      throw ServiceException.FAILURE("crmAccountDAOImpl.editAccounts : " + e.getMessage(), e);
    }
    return new KwlReturnObject(true, KWLErrorMsgs.S01, "", ll, dl);
  }