@Override public long insert(Category category) throws BadRequestServiceEx, NotFoundServiceEx { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Persisting Category ... "); } if (category == null) { throw new BadRequestServiceEx("Category type must be specified !"); } Category cat = new Category(); cat.setName(category.getName()); categoryDAO.persist(cat); // // // // Persisting SecurityRule // // // List<SecurityRule> rules = category.getSecurity(); // // if (rules != null) { // for (SecurityRule rule : rules) { // rule.setCategory(cat); // securityDAO.persist(rule); // } // } return cat.getId(); }
private Resource rbr2res(RESTBackupResource rbr, long catId) { Resource ret = Convert.convertResource(rbr.getResource()); ret.getCategory().setId(catId); // remap category return ret; }
if (category.getId() != null) { loadedCategory = categoryDAO.find(category.getId()); if (loadedCategory == null) { throw new NotFoundServiceEx("Resource Category not found [id:" + category.getId() + "]"); } else if (category.getName() != null) { Search searchCriteria = new Search(Category.class); searchCriteria.addFilterEqual("name", category.getName()); + category.getName() + "]");
private RESTBackupCategory createCategory(Category category) throws BadRequestServiceEx, InternalErrorServiceEx { LOGGER.info("Packing category " + category.getName()); RESTBackupCategory ret = new RESTBackupCategory(); ret.setName(category.getName()); SearchFilter filter = new CategoryFilter(category.getName(), SearchOperator.EQUAL_TO); List<Resource> resList = resourceService.getResourcesFull(filter, null); for (Resource resource : resList) { RESTBackupResource rbe = createResource(resource); ret.addResource(rbe); } return ret; }
public static Resource convertResource(RESTResource resource) { Category category = new Category(); if (resource.getCategory().getName() != null) category.setName(resource.getCategory().getName()); if (resource.getCategory().getId() != null) category.setId(resource.getCategory().getId()); Resource r = new Resource(); r.setDescription(resource.getDescription()); r.setMetadata(resource.getMetadata()); r.setName(resource.getName()); r.setCategory(category); // Parsing Attributes list if (CollectionUtils.isNotEmpty(resource.getAttribute())) { List<Attribute> attributes = Convert.convertAttributeList(resource.getAttribute()); r.setAttribute(attributes); } RESTStoredData dataDto = resource.getStore(); if (dataDto != null) { StoredData data = new StoredData(); data.setData(dataDto.getData()); r.setData(data); } return r; }
@Override public String quickRestore(SecurityContext sc, RESTQuickBackup backup) { if (LOGGER.isDebugEnabled()) LOGGER.debug("quickRestore()"); try { LOGGER.error("Deleting all categories"); for (Category category : categoryService.getAll(null, null)) { categoryService.delete(category.getId()); } LOGGER.error("Deleting all resources"); for (ShortResource shortResource : resourceService.getAll(null, null, null)) { resourceService.delete(shortResource.getId()); } for (RESTBackupCategory rbc : backup.getCategories()) { quickRestoreCategory(rbc); } return "ok"; } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); throw new InternalErrorWebEx(ex.getMessage()); } }
RESTCategory newrc = resource.getCategory(); Category oldrc = old.getCategory(); if ((newrc.getId() != null && !newrc.getId().equals(oldrc.getId())) || (newrc.getName() != null && !newrc.getName().equals(oldrc.getName()))) { LOGGER.info("Trying to change category old(" + oldrc.getId() + ":" + oldrc.getName() + ") new(" + newrc.getId() + ":" + newrc.getName() + ")"); throw new BadRequestWebEx("Can't change resource category");
public UNREDDResource(Resource resource) { // category sanity check if(! getCategoryName().equals(resource.getCategory().getName())) { throw new IllegalArgumentException("Bad resource category " + resource.getCategory().getName() + ": only handling " + getCategoryName()); } // copy attribs if (resource.getAttribute() != null) { // SG - when there are no attributes, resource.getAttribute() returns null - can't modify resource.getAttribute() for (Attribute a : resource.getAttribute()) { originalAttributes.put(a.getName(), a); DataType type = getAttributeMap().get(a.getName()); if(type == null) { if( ! getReverseAttributes().contains(a.getTextValue())) { LOGGER.error("Bad attribute name " + a + " for class " + this.getClass().getSimpleName()); continue; } } else if( type != a.getType()) { LOGGER.error("Bad attribute type " + a + " for class " + this.getClass().getSimpleName()); continue; } attributes.put(a.getName(), a.getValue()); } } // TODO: copy other resource info }
@Override public Category get(SecurityContext sc, long id) throws NotFoundWebEx { if (id == -1) { if (LOGGER.isDebugEnabled()) LOGGER.debug("Retriving dummy data !"); // // return test instance // Category category = new Category(); category.setName("dummy name"); return category; } Category ret = categoryService.get(id); if (ret == null) throw new NotFoundWebEx("Category not found"); return ret; }
@Override public long update(SecurityContext sc, long id, Category category) { try { Category old = categoryService.get(id); if (old == null) throw new NotFoundWebEx("Category not found"); // // Authorization check. // boolean canUpdate = false; User authUser = extractAuthUser(sc); canUpdate = resourceAccessWrite(authUser, old.getId()); if (canUpdate) { id = categoryService.update(category); } else { throw new ForbiddenErrorWebEx("This user cannot update this category !"); } } catch (BadRequestServiceEx e) { throw new BadRequestWebEx(e.getMessage()); } return id; }
private void quickRestoreCategory(RESTBackupCategory rbc) throws BadRequestServiceEx, NotFoundServiceEx, DuplicatedResourceNameServiceEx { LOGGER.info("Restoring category: " + rbc.getName()); Category cat = rbc2cat(rbc); long catId = categoryService.insert(cat); // TODO: cat auth for (RESTBackupResource rbr : rbc.getResources()) { if (LOGGER.isInfoEnabled()) { int attnum = (rbr != null && rbr.getResource() != null && rbr.getResource() .getAttribute() != null) ? rbr.getResource().getAttribute().size() : -1; LOGGER.info("Restoring resource: " + cat.getName() + ":" + rbr.getResource().getName() + " (" + attnum + " attrs)"); } Resource res = rbr2res(rbr, catId); resourceService.insert(res); // TODO: res auth } }
@Override public long insert(SecurityContext sc, Category category) { if (category == null) throw new BadRequestWebEx("Category is null"); if (category.getId() != null) throw new BadRequestWebEx("Id should be null"); long id = -1; // // Preparing Security Rule // // User authUser = extractAuthUser(sc); // SecurityRule securityRule = new SecurityRule(); // securityRule.setCanRead(true); // securityRule.setCanWrite(true); // securityRule.setUser(authUser); // // List<SecurityRule> securities = new ArrayList<SecurityRule>(); // securities.add(securityRule); // // category.setSecurity(securities); try { id = categoryService.insert(category); } catch (NotFoundServiceEx e) { throw new NotFoundWebEx(e.getMessage()); } catch (BadRequestServiceEx e) { throw new BadRequestWebEx(e.getMessage()); } return id; }
private RESTResource createRESTResource(Resource resource) { RESTResource ret = new RESTResource(); ret.setCategory(new RESTCategory(resource.getCategory().getName())); ret.setName(resource.getName()); ret.setDescription(resource.getDescription()); ret.setMetadata(resource.getMetadata()); if (resource.getData() != null) ret.setData(resource.getData().getData()); if (CollectionUtils.isNotEmpty(resource.getAttribute())) ret.setAttribute(Convert.convertToShortAttributeList(resource.getAttribute())); return ret; }