Search in sources :

Example 1 with MessageDescriptorContainer

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();
}
Also used : SClass(org.bimserver.shared.meta.SClass) MessageDescriptorContainer(org.bimserver.shared.pb.ProtocolBuffersMetaData.MessageDescriptorContainer) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) Builder(com.google.protobuf.Message.Builder) ArrayList(java.util.ArrayList) SField(org.bimserver.shared.meta.SField) ByteString(com.google.protobuf.ByteString) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) SBase(org.bimserver.shared.meta.SBase) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor)

Aggregations

ByteString (com.google.protobuf.ByteString)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 Builder (com.google.protobuf.Message.Builder)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DataHandler (javax.activation.DataHandler)1 SBase (org.bimserver.shared.meta.SBase)1 SClass (org.bimserver.shared.meta.SClass)1 SField (org.bimserver.shared.meta.SField)1 MessageDescriptorContainer (org.bimserver.shared.pb.ProtocolBuffersMetaData.MessageDescriptorContainer)1