1 // Copyright 2007 Google Inc. All rights reserved.
3 package com
.google
.appengine
.api
.datastore
;
5 import com
.google
.common
.collect
.Maps
;
6 import com
.google
.storage
.onestore
.v3
.OnestoreEntity
.EntityProto
;
7 import com
.google
.storage
.onestore
.v3
.OnestoreEntity
.Path
;
8 import com
.google
.storage
.onestore
.v3
.OnestoreEntity
.Reference
;
10 import java
.util
.Collection
;
14 * {@code EntityTranslator} contains the logic to translate an {@code
15 * Entity} into the protocol buffers that are used to pass it to the
16 * implementation of the API.
19 public class EntityTranslator
{
21 public static Entity
createFromPb(EntityProto proto
, Collection
<Projection
> projections
) {
22 Key key
= KeyTranslator
.createFromPb(proto
.getKey());
24 Entity entity
= new Entity(key
);
25 Map
<String
, Object
> values
= Maps
.newHashMap();
26 DataTypeTranslator
.extractPropertiesFromPb(proto
, values
);
27 for (Projection projection
: projections
) {
28 entity
.setProperty(projection
.getName(), projection
.getValue(values
));
33 public static Entity
createFromPb(EntityProto proto
) {
34 Key key
= KeyTranslator
.createFromPb(proto
.getKey());
36 Entity entity
= new Entity(key
);
37 DataTypeTranslator
.extractPropertiesFromPb(proto
, entity
.getPropertyMap());
41 public static Entity
createFromPbBytes(byte[] pbBytes
) {
42 EntityProto proto
= new EntityProto();
43 boolean parsed
= proto
.mergeFrom(pbBytes
);
44 if (!parsed
|| !proto
.isInitialized()) {
45 throw new IllegalArgumentException("Could not parse EntityProto bytes");
47 return createFromPb(proto
);
50 public static EntityProto
convertToPb(Entity entity
) {
51 Reference reference
= KeyTranslator
.convertToPb(entity
.getKey());
53 EntityProto proto
= new EntityProto();
54 proto
.setKey(reference
);
56 Path entityGroup
= proto
.getMutableEntityGroup();
57 Key key
= entity
.getKey();
58 if (key
.isComplete()) {
59 entityGroup
.addElement(reference
.getPath().elements().get(0));
62 DataTypeTranslator
.addPropertiesToPb(entity
.getPropertyMap(), proto
);
66 private EntityTranslator() {