/** (内部)搜索 */
  private Representation searchOrderList(
      String firstType,
      String firstValue,
      String secondType,
      String secondValue,
      int itemsPerPage,
      int pageNum,
      boolean isLike) {

    ArrayList<SLOrder> listOrders =
        (ArrayList<SLOrder>)
            orderDao.searchOrderList(
                firstType, firstValue, secondType, secondValue, itemsPerPage, pageNum, isLike);
    OrderPage orderPage = new OrderPage(itemsPerPage, pageNum);
    orderPage.setTheOrders(listOrders);
    long totalNum = orderDao.selectCount(firstType, firstValue, secondType, secondValue, isLike);
    if (totalNum % itemsPerPage == 0) {
      orderPage.setTotalPageNum((int) totalNum / itemsPerPage);
    } else {
      orderPage.setTotalPageNum((int) totalNum / itemsPerPage + 1);
    }
    JsonRepresentation ret = new JsonRepresentation(orderPage);
    try {
      ret.getJsonObject().put(RestCallInfo.REST_STATUS, RestCallStatus.success);
      ret.getJsonObject().put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.no_error);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return ret;
  }
  @Put
  public Representation baixarConta(Representation r) throws IOException, JSONException {
    JsonRepresentation jsonRep = new JsonRepresentation(r);
    JSONObject json = jsonRep.getJsonObject();
    String numeroDocumento = json.getString("numeroDocumento");

    boolean result = contaDao.baixarConta(numeroDocumento);
    if (result == true) {
      return new StringRepresentation("true");
    } else {
      return new StringRepresentation("false");
    }
  }
Example #3
0
  @Post
  public StringRepresentation destroy(Representation entity) {
    StringRepresentation representation = null;

    JsonRepresentation represent;
    try {
      represent = new JsonRepresentation(entity);
      UserSessions sessions = UserSessions.getInstance();
      User user = sessions.getUserByRequest(getRequest());
      JSONObject obj = represent.getJsonObject();
      ClusterDao dao = new ClusterDao();

      if (user != null) {
        int clusterID = Integer.valueOf(obj.get("0") + "");
        String pwd = obj.get("1") + "";
        if (pwd.equals("")) pwd = user.getCloudSecure();

        ClusterConfiguration clusterConfig = dao.findSpecificCluster(user.getId(), clusterID);
        if (clusterConfig != null) {
          ConnectionUtil.getInputStream(clusterConfig.getCloudUsername(), pwd);

          if (clusterConfig.getState() == ClusterConfiguration.UP) {
            clusterConfig.setCloudPassword(pwd);
            clusterConfig.setActionType(ClusterConfiguration.DESTROY_CLUSTER);
            clusterConfig.setCloudgeneUser(user);
            clusterConfig.setState(ClusterConfiguration.QUEUE);

            // that it doesn't appear in queue / db at the same time
            dao.updateCluster(clusterConfig.getPk(), 1);
            // add to queue
            ClusterQueue.getInstance().submit(clusterConfig);

            // add to threadpool
            ClusterTask task = new ClusterTask(clusterConfig);
            ClusterThreadPoolDelete.getInstance().runTask(task);
          }

          getResponse().setStatus(Status.SUCCESS_OK);
        }
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      getResponse().setEntity(representation);
      return representation;
    }
    return representation;
  }
  private Map<String, Object> toMap(JsonRepresentation entity) throws BadRequestException {
    if (entity == null) {
      return Collections.emptyMap();
    }

    try {
      final String jsonString = entity.getJsonObject().toString();
      if (StringUtils.isNotEmpty(jsonString)) {
        JsonValue jsonContent = JsonValueBuilder.toJsonValue(jsonString);
        return jsonContent.asMap(Object.class);
      }

      return Collections.emptyMap();
    } catch (JSONException e) {
      throw new BadRequestException(e.getMessage());
    }
  }
  @Post
  public Representation criarContaAPagar(Representation r) throws IOException, JSONException {
    String tipoConta = (String) getRequestAttributes().get("tipoConta");
    JsonRepresentation jsonRep = new JsonRepresentation(r);
    JSONObject json = jsonRep.getJsonObject();

    Promissoria p = new Promissoria();

    String numero = json.getString("numeroDocumento");
    String dataEmissao = json.getString("dataEmissao");
    String dataPagamento = json.getString("dataPagamento");
    String descricao = json.getString("descricao");
    String fornecedor = json.getString("fornecedor");

    if (tipoConta.equals("promissoria")) {
      p = new Promissoria();
    } else {
      String banco = json.getString("banco");
      Duplicata d = new Duplicata();
      d.setBanco(BancoDePagamento.valueOf(banco));
      p = d;
    }

    p.setNumeroDocumento(numero);
    p.setDataEmissao(dataEmissao);
    p.setDataPagamento(dataPagamento);
    p.setDescricao(descricao);
    p.setFornecedor(fornecedor);
    // p.setStatus(Status.valueOf(status));

    boolean result = contaDao.cadastroConta(p);
    if (result == true) {
      return new StringRepresentation("true");
    } else {
      return new StringRepresentation("false");
    }
  }
  /** 获取预订信息 */
  @GET
  @Path("getorderinfo/{orderId}")
  @Produces("application/json")
  public Representation getOrderInfo(@PathParam("orderId") int orderId) {
    SLOrder slOrder = orderDao.getSLOrderByOrderId(orderId);
    if (slOrder == null) {
      HashMap returnInfo = new HashMap();
      returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail);
      returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.no_such_order);
      return new JsonRepresentation(returnInfo);
    }

    slOrder.setTheBook(bookDao.queryByISBN(slOrder.getBookISBN()));
    slOrder.setTheUser(userDao.getSLUserByEmail(slOrder.getUserEmail()));

    JsonRepresentation ret = new JsonRepresentation(slOrder);
    try {
      ret.getJsonObject().put(RestCallInfo.REST_STATUS, RestCallStatus.success);
      ret.getJsonObject().put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.no_error);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return ret;
  }
Example #7
0
  @Post
  public Representation acceptRepresentation(Representation entity) {
    Representation representation = null;
    try {
      JsonRepresentation represent = new JsonRepresentation(entity);

      UserSessions sessions = UserSessions.getInstance();
      User user = sessions.getUserByRequest(getRequest());

      JSONObject obj = represent.getJsonObject();
      if (user != null) {
        ClusterConfiguration clusterConfig = new ClusterConfiguration();
        String usr = obj.get("loginUsername").toString();
        String pwd = obj.get("loginPassword").toString();
        String program = obj.get("program").toString();
        String ssh = obj.get("key").toString();
        String name = obj.get("name").toString();
        String provider = obj.get("cluster").toString();
        String amount = obj.get("amount").toString();
        String bucket = obj.get("bucketName").toString();
        String type = obj.get("type").toString();
        String dirLog = "logs";

        /** check credentials */
        ConnectionUtil.getInputStream(usr, pwd);
        UserDao dao = new UserDao();
        if (!obj.has("saveCre")) {
          user.setCloudKey("");
          user.setCloudSecure("");
          dao.updateCredential(user);
        } else {
          user.setCloudKey(usr);
          user.setCloudSecure(pwd);
          dao.updateCredential(user);
        }
        user.setCloudKey(usr);
        user.setCloudSecure(pwd);

        /** save ssh data */
        if (ssh.equals("1")) {
          createKey(clusterConfig, user);
        } else if (ssh.equals("3")) {
          clusterConfig.setSshPrivate(user.getSshKey());
          clusterConfig.setSshPublic(user.getSshPub());
        }
        if (obj.has("saveSsh")) {
          dao.updateSSH(user);
        }
        if (obj.has("s3Export")) {
          clusterConfig.setS3Bucket(bucket);
        }

        /** CONFIGURE CLUSTER */
        Program prog = Programs.getProgramByName(program);
        clusterConfig.setProgram(prog.getCluster());
        Utils.checkDirAvailable(dirLog);
        clusterConfig.setLog(dirLog + File.separatorChar + System.currentTimeMillis() + ".txt");
        clusterConfig.setCloudID(String.valueOf(System.currentTimeMillis()));
        clusterConfig.setName(name);
        clusterConfig.getProgram().setProvider(provider);
        clusterConfig.setCloudUsername(usr);
        clusterConfig.setCloudPassword(pwd);
        clusterConfig.setAmount(Integer.valueOf(amount));
        clusterConfig.setCloudgeneUser(user);
        clusterConfig.setSSHAvailable(true);
        clusterConfig.setInstanceType(type);
        clusterConfig.setStartTime(System.currentTimeMillis());
        clusterConfig.setActionType(ClusterConfiguration.CREATE_CLUSTER);
        clusterConfig.setState(ClusterConfiguration.QUEUE);

        /** add to queue */
        ClusterQueue.getInstance().submit(clusterConfig);

        /** add to threadpool */
        ClusterTask task = new ClusterTask(clusterConfig);
        ClusterThreadPoolCreate.getInstance().runTask(task);
        getResponse().setStatus(Status.SUCCESS_OK);

      } else {
        representation = new StringRepresentation("No user");
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        getResponse().setEntity(representation);
        return representation;
      }
    } catch (S3ServiceException e) {
      representation = new StringRepresentation("Please check your security credentials");
      getResponse().setEntity(representation);
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      e.printStackTrace();
      return representation;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      representation = new StringRepresentation("Error occured");
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      getResponse().setEntity(representation);
      e.printStackTrace();
      return representation;
    }
    return representation;
  }