writeOperation = WriteOperation.PERSIST;
} else {
writeOperation = WriteOperation.UPDATE;
}
Splittable version = null;
if (writeOperation == WriteOperation.PERSIST
|| writeOperation == WriteOperation.UPDATE) {
/*
* If we're sending an operation, the domain object must be persistent.
* This means that it must also have a non-null version.
*/
Object domainVersion = service.getVersion(domainObject);
if (domainVersion == null) {
throw new UnexpectedException("The persisted entity with id "
+ service.getId(domainObject) + " has a null version", null);
}
version = returnState.flatten(domainVersion);
}
boolean inResponse = bean.getTag(Constants.IN_RESPONSE) != null;
/*
* Don't send any data back to the client for an update on an object that
* isn't part of the response payload when the client's version matches
* the domain version.
*/
if (WriteOperation.UPDATE.equals(writeOperation) && !inResponse) {
String previousVersion = bean.<String> getTag(Constants.VERSION_PROPERTY_B64);
if (version != null && previousVersion != null
&& version.equals(fromBase64(previousVersion))) {
continue;
}
}
OperationMessage op = FACTORY.operation().as();
/*
* Send a client id if the id is ephemeral or was previously associated
* with a client id.
*/
if (id.wasEphemeral()) {
op.setClientId(id.getClientId());
}
op.setOperation(writeOperation);
// Only send properties for entities that are part of the return graph
if (inResponse) {
Map<String, Splittable> propertyMap = new LinkedHashMap<String, Splittable>();
// Add all non-null properties to the serialized form
Map<String, Object> diff = AutoBeanUtils.getAllProperties(bean);
for (Map.Entry<String, Object> d : diff.entrySet()) {
Object value = d.getValue();
if (value != null) {
propertyMap.put(d.getKey(), EntityCodex.encode(returnState, value));
}
}
op.setPropertyMap(propertyMap);
}
if (!id.isEphemeral() && !id.isSynthetic()) {
// Send the server address only for persistent objects
op.setServerId(toBase64(id.getServerId()));
}
if (id.isSynthetic()) {
op.setStrength(Strength.SYNTHETIC);
op.setSyntheticId(id.getSyntheticId());
} else if (id.isEphemeral()) {
op.setStrength(Strength.EPHEMERAL);
}
op.setTypeToken(service.resolveTypeToken(id.getProxyClass()));
if (version != null) {
op.setVersion(toBase64(version.getPayload()));
}
operations.add(op);
}
}