Exemple #1
0
  public HashMap<String, Object> saveData(
      String formDatas, String mode, FileUploadBean file, String lang) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    HashMap<String, Object> datas = null;
    HashMap<String, Object> result = null;
    HashMap<String, Object> resultdata = null;
    String sql = null;
    boolean boolResult = false;
    String resultMsg = "";
    String newid = null;
    String filePath = null;
    String filename = null;
    String mapkey = null;
    String iconname = null;
    String[][] langValue = null;
    String[] langtmp = null;

    try {
      datas = DCIString.jsonTranToObjMap(formDatas); // new
      // ObjectMapper().readValue(formDatas,
      // HashMap.class);
      conn = this.dbobj.getConnection(ConnectionType.SYS);

      if (mode.equals("Add")) {
        sql = this.cmd.addData();
        newid = getMaxID(conn);
        if (file == null || file.getFile() == null) {
          throw new Exception("no file upload");
        } else {
          filename = file.getFile().getOriginalFilename();
          filename = newid + filename.substring(filename.lastIndexOf(46));
          filePath =
              new ConfigControl(false, null).getConfigXMLPath()
                  + File.separator
                  + APConstants.getUploadicondir()
                  + File.separator
                  + filename;
          if (new APPubMethods().saveUploadFile(filename, file)) {
            throw new Exception("save file fail");
          }
        }
        mapkey = getMaxKey(conn);
      } else {
        sql = this.cmd.updateData();
        newid = datas.get("icon_id").toString();
        filePath = datas.get("icon_path").toString();
        mapkey = datas.get("icon_map_key").toString();
      }

      if (datas.get("icon_name") == null
          || DCIString.isNullOrEmpty(datas.get("icon_name").toString())) {
        iconname = newid;
      } else {
        iconname = datas.get("icon_name").toString();
      }

      ps =
          conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

      ps.setString(1, iconname);
      ps.setString(2, mapkey);
      ps.setString(3, filePath);
      ps.setString(4, datas.get("icon_type").toString());
      ps.setString(5, newid);
      ps.executeUpdate();

      langValue = new String[2][];

      if (datas.get("icon_names") == null
          || DCIString.isNullOrEmpty(datas.get("icon_names").toString())
          || datas.get("icon_names").toString().indexOf("$") == -1
          || datas.get("icon_names").toString().length() == 1) {
        langValue[0] = new String[] {"CHS", iconname, ""};
        langValue[1] = new String[] {"CHT", iconname, ""};
      } else {
        langtmp = datas.get("icon_names").toString().split("\\$");
        if (langtmp.length == 2) {
          langValue[0] =
              new String[] {"CHS", iconname, datas.get("icon_names").toString().split("\\$")[0]};
          langValue[1] =
              new String[] {"CHT", iconname, datas.get("icon_names").toString().split("\\$")[1]};
        } else {
          if (datas.get("icon_names").toString().startsWith("$")) {
            langValue[0] = new String[] {"CHS", iconname, ""};
            langValue[1] =
                new String[] {"CHT", iconname, datas.get("icon_names").toString().split("\\$")[1]};
          } else {
            langValue[0] =
                new String[] {"CHS", iconname, datas.get("icon_names").toString().split("\\$")[0]};
            langValue[1] = new String[] {"CHT", iconname, ""};
          }
        }
      }

      saveLangs(conn, langValue);
      resultdata = getAllData("1", "1", " and icon_id = '" + newid + "'", lang);

      boolResult = true;
    } catch (Exception ex) {
      ex.printStackTrace();
      boolResult = false;
      resultMsg = ex.getMessage();
    } finally {
      this.dbctrl.closeConnection(rs, ps, conn);
      result = new HashMap<String, Object>();
      result.put("success", boolResult);
      result.put("resultData", resultdata);
      result.put("errorMessage", resultMsg);
    }
    return result;
  }