@Override
public String memento(Object viewModelPojo) {
final MementoService mementoService = servicesInjector.lookupService(MementoService.class);
final BookmarkService bookmarkService = servicesInjector.lookupService(BookmarkService.class);
final MementoService.Memento memento = mementoService.create();
// this is horrible, but there's a catch-22 here...
// we need an adapter in order to query the state of the object via the metamodel, on the other hand
// we can't create an adapter without the identifier, which is what we're trying to derive
// so... we create a temporary transient adapter, use it to wrap this adapter and interrogate this pojo,
// then throw away that adapter (remove from the adapter map)
boolean createdTemporaryAdapter = false;
ObjectAdapter viewModelAdapter = adapterManager.getAdapterFor(viewModelPojo);
if(viewModelAdapter == null) {
final ObjectSpecification objectSpecification = specificationLoader.loadSpecification(viewModelPojo.getClass());
final ObjectSpecId objectSpecId = objectSpecification.getSpecId();
viewModelAdapter = adapterManager.mapRecreatedPojo(RootOidDefault.create(objectSpecId, UUID.randomUUID().toString()), viewModelPojo);
createdTemporaryAdapter = true;
}
try {
final ObjectSpecification spec = viewModelAdapter.getSpecification();
final List<OneToOneAssociation> properties = spec.getProperties(Contributed.EXCLUDED);
for (OneToOneAssociation property : properties) {
// ignore read-only
if(!property.containsDoOpFacet(PropertySetterFacet.class)) {
continue;
}
// ignore those explicitly annotated as @NotPersisted
if(property.isNotPersisted()) {
continue;
}
// otherwise, include
final ObjectAdapter propertyValueAdapter = property.get(adapterManager.adapterFor(viewModelPojo));
if(propertyValueAdapter != null) {
final Object propertyValue = propertyValueAdapter.getObject();
if(mementoService.canSet(propertyValue)) {
memento.set(property.getId(), propertyValue);
} else {
final Bookmark propertyValueBookmark = bookmarkService.bookmarkFor(propertyValue);
memento.set(property.getId() + ".bookmark", propertyValueBookmark);
}
}
}
return memento.asString();