public TaiLieuDinhKem getPhienBanMoiNhatTheoMa(Long userId, String code) {
    // TODO Auto-generated method stub
    DanhMucGiayTo type = DanhMucGiayToLocalServiceUtil.getGiayToBoiMa(code);
    TaiKhoanNguoiDung account = SharedserviceUtil.getTaiKhoanNguoiDungByLiferayId(userId);
    if (account == null) return null;

    if (type != null) {
      try {
        TaiLieuChungThuc chungThuc =
            TaiLieuChungThucLocalServiceUtil.getCurrentVersion(type.getId(), account.getId());
        if (chungThuc != null) {
          TaiLieuDinhKem dinhKem = new TaiLieuDinhKem(DanhMucGiayToSoap.toSoapModel(type));
          dinhKem.setTailieuChungThuc(TaiLieuChungThucSoap.toSoapModel(chungThuc));
          dinhKem.setUrl(
              WebserviceFactory.getThamSoService().getValue(Constants.ThamSo.DOMAIN)
                  + storage.getURLById(chungThuc.getNoiLuuTruId()));
          return dinhKem;
        }
      } catch (Exception e) {
        // TODO: handle exception
      }
    }

    return null;
  }
  public TaiLieuDinhKem getPhienBanMoiNhatTheoGiayTo(Long userId, Long giayToId) {
    try {
      TaiKhoanNguoiDung account = SharedserviceUtil.getTaiKhoanNguoiDungByLiferayId(userId);
      if (account == null) return null;

      DanhMucGiayTo danhMucGiayTo = DanhMucGiayToLocalServiceUtil.getDanhMucGiayTo(giayToId);
      if (danhMucGiayTo != null) {
        return getPhienBanMoiNhatTheoMa(account.getId(), danhMucGiayTo.getMa());
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block

    }
    return null;
  }
  public boolean doDelete(Long userId, String documentTypeCode) {
    // TODO Auto-generated method stub
    // get last version

    DanhMucGiayTo type = null;
    try {
      type = DanhMucGiayToLocalServiceUtil.getGiayToBoiMa(documentTypeCode);

    } catch (Exception e) {
      // TODO: handle exception
    }
    TaiKhoanNguoiDung account = SharedserviceUtil.getTaiKhoanNguoiDungByLiferayId(userId);
    if (account == null) return false;

    // get last version
    TaiLieuChungThuc lastDocument = null;

    try {
      lastDocument =
          TaiLieuChungThucLocalServiceUtil.getCurrentVersion(type.getId(), account.getId());

    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }

    if (lastDocument == null) return true;

    if (lastDocument.getTrangThai() == Constants.DOCUMENT_NEW) {

      try {
        lastDocument = TaiLieuChungThucLocalServiceUtil.delete(lastDocument.getId());
        if (lastDocument != null) {
          try {
            storage.deleteFile(lastDocument.getNoiLuuTruId());
          } catch (Exception e) {
            // TODO: handle exception
          }
        }
        return true;
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return false;
  }
  public ResultUpload doUpload(
      Long userId,
      Long thutucId,
      String documentTypeCode,
      byte[] b,
      String fileName,
      boolean overwriteIfUsed,
      boolean overwriteIfApproved) {
    // TODO Auto-generated method stub
    ResultUpload result = new ResultUpload();
    log.info(
        "1.-----------------start upload file with:userid,thutucId,codeId,filename:"
            + userId
            + ":"
            + thutucId
            + ":"
            + documentTypeCode
            + ":"
            + fileName);
    try {

      DanhMucGiayTo type = null;
      try {
        type = DanhMucGiayToLocalServiceUtil.getGiayToBoiMa(documentTypeCode);

      } catch (Exception e) {
        // TODO: handle exception
        result.setCode(Constants.UPLOAD_DOCUMENT_NOTFOUND_GIAYTO);
        return result;
      }

      log.info("2.------kiem tra tai khoan-");
      // get last version
      Long taikhoanId = userId;
      try {

        TaiKhoanNguoiDung account = SharedserviceUtil.getTaiKhoanNguoiDungByLiferayId(userId);
        if (account != null) {
          taikhoanId = account.getId();

        } else {
          result.setCode(Constants.UPLOAD_DOCUMENT_NOTFOUND_ACCOUNT_BY_USER);
          return result;
        }
      } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        result.setCode(Constants.UPLOAD_DOCUMENT_NOTFOUND_ACCOUNT_BY_USER);
        return result;
      }
      TaiLieuChungThuc lastDocument = null;
      log.info("3.------lay ve version-");
      int version = 1;
      try {
        lastDocument = TaiLieuChungThucLocalServiceUtil.getCurrentVersion(type.getId(), taikhoanId);
        version = TaiLieuChungThucLocalServiceUtil.getMaxVersion(taikhoanId, type.getId());
        version = version + 1;
      } catch (Exception e) {
        // TODO: handle exception
      }

      if (type == null) {
        result.setCode(Constants.UPLOAD_DOCUMENT_TYPE_NOT_FOUND);

        return result;
      } else {
        log.info("4. xu ly ten file");
        String vFile = "" + version;
        try {
          vFile =
              vFile + "_" + CounterLocalServiceUtil.increment(DocumentServiceImpl.class.getName());
        } catch (Exception e1) {
          // TODO Auto-generated catch block

        }

        String typeFile = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        fileName = type.getMa().toUpperCase() + "_V" + vFile + "." + typeFile;
        typeFile = typeFile.toLowerCase().trim();
        if (type.getKieuTaiLieu().trim().length() != 0
            && type.getKieuTaiLieu().trim().toLowerCase().indexOf(typeFile) == -1) {
          // not config type format for file
          result.setCode(Constants.UPLOAD_FILE_TYPE);
          return result;
        }
      }

      if (lastDocument != null) {

        if (lastDocument.getTrangThai() == Constants.DOCUMENT_APPROVED) {
          if (!overwriteIfApproved) {
            result.setCode(Constants.UPLOAD_FILE_APPROVED);
            return result;
          }

        } else if (lastDocument.getTrangThai() == Constants.DOCUMENT_NEW) {
          log.info("5.------xoa file-");
          deleteTaiLieuChungThuc(lastDocument);
        }
      }
      Long folderId =
          Long.valueOf(new ThamSoServiceImpl().getValue(Constants.ThamSo.CMON_DOCUMENT_FOLDER_ID));
      Long limit = 99999999999999L;
      try {
        limit = Long.valueOf(type.getGioiHanDungLuong());

      } catch (Exception e) {
        // TODO: handle exception
      }
      log.info("5. xu ly upload");
      ResultUpload upload = storage.upload(userId, folderId, b, fileName, limit);

      if (upload.getCode().equals(Constants.UPLOAD_OK)) {

        TaiLieuChungThuc document = new TaiLieuChungThucClp();
        document.setNoiLuuTruId(upload.getFileId());
        document.setTen(type.getTen());
        document.setNguoiUploadId(taikhoanId);
        document.setChuSohuuId(taikhoanId);
        // document.setHoSoTTHCCId(1L);
        // document.setNguoiduyetId(userId);

        document.setNgayUpload(new Date());
        document.setPhienBan(version);
        document.setDanhMucGiayToId(type.getId());
        // document.setUrl(upload.getUrl());
        document.setTrangThai(Constants.DOCUMENT_NEW);

        try {
          log.info("6. them moi");

          document = TaiLieuChungThucLocalServiceUtil.add(document);
          log.info("6.1 da qua phan them moi");
          result.setCode(Constants.UPLOAD_OK);
          result.setDocument(TaiLieuChungThucSoap.toSoapModel(document));
          result.setFileId(upload.getFileId());
          result.setUrl(
              WebserviceFactory.getThamSoService().getValue(Constants.ThamSo.DOMAIN)
                  + upload.getUrl());
          result.setResult(true);
          Long tt2GiayToId = getIDTTHC2GiayTo(thutucId, document.getId());
          if (tt2GiayToId != null) {
            result.setThuTuc2GiayToId(tt2GiayToId);
          }
          log.info("return result:" + result.toString());
          return result;
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          result.setCode(e.getMessage());
          return result;
        }

      } else {
        result.setCode(upload.getCode());
        return result;
      }

    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      result.setCode(e.getMessage());
      return result;
    }
  }