@Get public Representation searchPartners() { JSONArray response = new JSONArray(); try { int adminId = this.validateAuthentication(); PartnerSearchRepresentation p_sr = new PartnerSearchRepresentation(); this.loadRepresentation(p_sr); DebugLog.b_d( this.moduleId, this.apiId, this.reqId_get, adminId, this.getUserAgent(), p_sr.toJSON().toString()); ArrayList<Partner> searchResult = PartnerDaoService.searchPartner(p_sr); response = JSONGenerator.toJSON(searchResult); } catch (PseudoException e) { this.addCORSHeader(); return this.doPseudoException(e); } catch (Exception e) { return this.doException(e); } Representation result = new JsonRepresentation(response); this.addCORSHeader(); return result; }
@Post public Representation createPartner(Representation entity) { Map<String, String> props = new HashMap<String, String>(); try { this.checkFileEntity(entity); int adminId = this.validateAuthentication(); DebugLog.b_d( this.moduleId, this.apiId, this.reqId_post, adminId, this.getUserAgent(), "<Form>"); if (!MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) { throw new ValidationException("上传数据类型错误"); } Partner partner = Partner.getInstance(); partner.setStatus(AccountStatus.deleted); // initialize the reference at this earlier step partner.setReference(ReferenceGenerator.generatePartnerReference()); partner = PartnerDaoService.createPartner(partner); ArrayList<Long> idList = new ArrayList<Long>(); idList.add((long) partner.getPartnerId()); props = this.handleMultiForm(entity, idList, props); partner.loadFromMap(props); partner.setStatus(AccountStatus.activated); PartnerDaoService.updatePartner(partner); } catch (PseudoException e) { DebugLog.d(e); this.addCORSHeader(); return this.doPseudoException(e); } catch (Exception e) { DebugLog.d(e); return this.doException(e); } setStatus(Status.SUCCESS_OK); Representation result = new StringRepresentation("SUCCESS", MediaType.TEXT_PLAIN); this.addCORSHeader(); return result; }
@Put public Representation createCourse(Representation entity) { try { int partnerId = this.validateAuthentication(); int courseId = Integer.parseInt(this.getReqAttr("id")); JSONObject jsonCourse = this.getJSONObj(entity); DebugLog.b_d( this.moduleId, this.apiId, this.reqId_put, partnerId, this.getUserAgent(), jsonCourse.toString()); Course course = CourseDaoService.getCourseById(courseId); if (course.getPartnerId() != partnerId) { throw new ValidationException("只可以修改自己的课程"); } if (jsonCourse.has("partnerId") && jsonCourse.getInt("partnerId") != course.getPartnerId()) { throw new ValidationException("不可以更改课程所属机构"); } course.storeJSON(jsonCourse); CourseDaoService.updateCourse(course); } catch (PseudoException e) { DebugLog.d(e); this.addCORSHeader(); return this.doPseudoException(e); } catch (Exception e) { DebugLog.d(e); return this.doException(e); } setStatus(Status.SUCCESS_OK); Representation result = new StringRepresentation("SUCCESS", MediaType.TEXT_PLAIN); this.addCORSHeader(); return result; }