use of org.bimserver.shared.pb.ProtocolBuffersMetaData.MessageDescriptorContainer in project BIMserver by opensourceBIM.
the class ProtocolBuffersConverter method convertSObjectToProtocolBuffersObject.
@SuppressWarnings({ "unchecked", "rawtypes" })
public Message convertSObjectToProtocolBuffersObject(SBase object, SClass definedType) throws ConvertException {
Builder builder = null;
MessageDescriptorContainer messageDescriptor = protocolBuffersMetaData.getMessageDescriptor(definedType.getSimpleName());
if (messageDescriptor == null) {
throw new ConvertException("No message descriptor found with name " + definedType.getSimpleName());
}
Descriptor definedDescriptor = messageDescriptor.getDescriptor();
try {
builder = DynamicMessage.getDefaultInstance(definedDescriptor).newBuilderForType();
} catch (SecurityException e) {
throw new ConvertException(e);
} catch (IllegalArgumentException e) {
throw new ConvertException(e);
}
SClass sClass = object.getSClass();
SClass superClass = sClass;
while (superClass.getSuperClass() != null && superClass.getSuperClass().getInstanceClass() != Object.class) {
superClass = superClass.getSuperClass();
}
if (sClass != superClass && messageDescriptor.getField("__actual_type") != null) {
builder.setField(messageDescriptor.getField("__actual_type"), sClass.getSimpleName());
builder.setField(messageDescriptor.getField("__" + sClass.getSimpleName()), convertSObjectToProtocolBuffersObject(object, sClass));
}
for (SField field : definedType.getAllFields()) {
try {
Object value = object.sGet(field);
FieldDescriptor fieldDescriptor = messageDescriptor.getField(field.getName());
if (value != null) {
if (value.getClass().isPrimitive() || value.getClass() == String.class || value.getClass() == Long.class || value.getClass() == Double.class || value.getClass() == Integer.class || value.getClass() == Boolean.class) {
builder.setField(fieldDescriptor, value);
} else if (value.getClass().isEnum()) {
Enum eNum = (Enum) value;
int ordinal = eNum.ordinal();
EnumValueDescriptor findValueByNumber = fieldDescriptor.getEnumType().findValueByNumber(ordinal);
builder.setField(fieldDescriptor, findValueByNumber);
} else if (value.getClass() == Date.class) {
builder.setField(fieldDescriptor, ((Date) value).getTime());
} else if (value.getClass() == DataHandler.class) {
DataHandler dataHandler = (DataHandler) value;
InputStream inputStream = dataHandler.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, byteArrayOutputStream);
ByteString byteString = ByteString.copyFrom(byteArrayOutputStream.toByteArray());
builder.setField(fieldDescriptor, byteString);
} else if (List.class.isAssignableFrom(value.getClass())) {
List list = (List) value;
List newList = new ArrayList();
for (Object o : list) {
if (fieldDescriptor.getJavaType() == JavaType.MESSAGE) {
newList.add(convertSObjectToProtocolBuffersObject((SBase) o, field.getGenericType()));
} else {
newList.add(o);
}
}
builder.setField(fieldDescriptor, newList);
} else {
LOGGER.error("Unimplemented: " + fieldDescriptor.getName() + ": " + value);
}
}
} catch (SecurityException e) {
LOGGER.error("", e);
} catch (IllegalArgumentException e) {
LOGGER.error("", e);
} catch (IOException e) {
LOGGER.error("", e);
}
}
return builder.build();
}
Aggregations