/** * {@inheritDoc} <br> * * Update a person resource. */ @Override public void update(ResourceSnapshot resource, DataSet content, WriteSession session) throws InconsistentContentException, UnknownResourceException { DataSet dataSet = get(resource); logDebug(resource, "Enforcing consistency..."); enforceConsistency(resource,content,dataSet); try { logDebug(resource,"Persisting new state:%n%s",content); add(resource.name(),content); session.modify(resource); session.saveChanges(); } catch (Exception e) { // Recover if failed add(resource.name(),dataSet); logError(resource,e,"Something went wrong",e); throw new ApplicationRuntimeException("Update failed",e); } }
protected void enforceConsistency(ResourceSnapshot resource, DataSet content, DataSet dataSet) throws InconsistentContentException { ManagedIndividualId id = ManagedIndividualId.createId(resource.name(),PersonHandler.ID); ManagedIndividual stateIndividual = dataSet. individual( id, ManagedIndividual.class); Property stateProperty= stateIndividual.property(PersonHandler.READ_ONLY_PROPERTY); ValidationConstraint<Property> constraint=null; if(stateProperty!=null) { constraint=ValidationConstraintFactory.readOnlyProperty(stateProperty); } else { constraint=ValidationConstraintFactory.readOnlyProperty(id,PersonHandler.READ_ONLY_PROPERTY); } Validator helper = Validator. builder(). withPropertyConstraint(constraint). build(); ValidationReport report = helper.validate(content); if(!report.isValid()) { InconsistentContentException error = new InconsistentContentException("Validation failed: "+report.validationFailures(), Constraints.constraints()); logError(resource,error,"Something went wrong when validating %n%s",content); throw error; } }