@Override public <T> T fetch(Class<T> type, Object value) throws IllegalStateException { checkConfig(); DocumentModel doc = (DocumentModel) fetch(value); if (doc != null) { if (type.isInstance(doc)) { return type.cast(doc); } return doc.getAdapter(type); } return null; }
@Override public boolean validate(Object value) { return validate(value, null); }
@Override public String getConstraintErrorMessage(Object invalidValue, Locale locale) { checkConfig(); switch (mode) { case ID_ONLY_REF: case REPO_AND_ID_REF: return Helper.getConstraintErrorMessage(this, "id", invalidValue, locale); case PATH_ONLY_REF: case REPO_AND_PATH_REF: return Helper.getConstraintErrorMessage(this, "path", invalidValue, locale); default: return String.format("%s cannot resolve reference %s", getName(), invalidValue); } }
@Override public String getName() { checkConfig(); return NAME; }
@Override public boolean validate(Object value, Object context) { MutableBoolean validated = new MutableBoolean(); resolve(value, context, (session, docRef) -> { if (session.exists(docRef)) { validated.setTrue(); } }); return validated.isTrue(); }
@Override public Object fetch(Object value) { return fetch(value, null); }
checkConfig(); if (!(value instanceof String)) { return;
@Override public Object fetch(Object value, Object context) { MutableObject<DocumentModel> docHolder = new MutableObject<>(); resolve(value, context, (session, docRef) -> { if (session.exists(docRef)) { DocumentModel doc = session.getDocument(docRef); // detach because we're about to close the session doc.detach(true); docHolder.setValue(doc); } }); return docHolder.getValue(); }
@Override public Serializable getReference(Object entity) throws IllegalStateException { checkConfig(); if (entity instanceof DocumentModel) { DocumentModel doc = (DocumentModel) entity; switch (mode) { case ID_ONLY_REF: return doc.getId(); case PATH_ONLY_REF: return doc.getPath().toString(); case REPO_AND_ID_REF: return doc.getRepositoryName() + ":" + doc.getId(); case REPO_AND_PATH_REF: return doc.getRepositoryName() + ":" + doc.getPath().toString(); } } return null; }