switch (attribute.getType()) { case DATE: attribute.setDateValue(sdf.parse(value)); } catch (ParseException e) { throw new InternalErrorServiceEx("Error parsing attribute date value"); attribute.setNumberValue(Double.valueOf(value)); break; case STRING: attribute.setTextValue(value); break; default: throw new IllegalStateException("Unknown type " + attribute.getType()); return attribute.getId(); } else { Attribute attribute = new Attribute(); attribute.setType(type); attribute.setName(name); attribute.setResource(resourceDAO.find(id)); attribute.setDateValue(sdf.parse(value)); } catch (ParseException e) { throw new InternalErrorServiceEx("Error parsing attribute date value"); attribute.setNumberValue(Double.valueOf(value)); break;
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 long updateAttribute(long id, String name, String value) throws InternalErrorServiceEx { Search searchCriteria = new Search(Attribute.class); searchCriteria.addFilterEqual("resource.id", id); searchCriteria.addFilterEqual("name", name); List<Attribute> attributes = this.attributeDAO.search(searchCriteria); Attribute attribute = attributes.get(0); switch (attribute.getType()) { case DATE: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); try { attribute.setDateValue(sdf.parse(value)); } catch (ParseException e) { throw new InternalErrorServiceEx("Error parsing attribute date value"); } break; case NUMBER: attribute.setNumberValue(Double.valueOf(value)); break; case STRING: attribute.setTextValue(value); break; default: throw new IllegalStateException("Unknown type " + attribute.getType()); } attribute = this.attributeDAO.merge(attribute); return attribute.getId(); }
public static Attribute convertAttribute(ShortAttribute shattr) throws InternalErrorWebEx { Attribute ret = new Attribute(); ret.setName(shattr.getName()); ret.setType(shattr.getType()); if (shattr.getType() == null) throw new BadRequestWebEx("Missing type for attribute " + shattr); switch (ret.getType()) { case DATE: try { ret.setDateValue(Attribute.DATE_FORMAT.parse(shattr.getValue())); } catch (ParseException e) { throw new BadRequestWebEx("Error parsing attribute date value " + shattr); } break; case NUMBER: try { ret.setNumberValue(Double.valueOf(shattr.getValue())); } catch (NumberFormatException ex) { throw new BadRequestWebEx("Error parsing number value " + shattr); } break; case STRING: ret.setTextValue(shattr.getValue()); break; default: throw new InternalErrorWebEx("Unknown attribute type " + shattr); } return ret; }
@Override public void updateAttributes(long id, List<Attribute> attributes) throws NotFoundServiceEx { Resource resource = resourceDAO.find(id); if (resource == null) { throw new NotFoundServiceEx("Resource not found " + id); } // // Removing old attributes // List<Attribute> oldList = resource.getAttribute(); if (oldList != null) { for (Attribute a : oldList) { attributeDAO.removeById(a.getId()); } } // // Saving old attributes // for (Attribute a : attributes) { a.setResource(resource); attributeDAO.persist(a); } }