1 56 package org.objectstyle.cayenne.service; 57 58 import java.util.ArrayList ; 59 import java.util.List ; 60 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 import org.objectstyle.cayenne.QueryResponse; 63 import org.objectstyle.cayenne.access.DataDomain; 64 import org.objectstyle.cayenne.access.DataRowStore; 65 import org.objectstyle.cayenne.access.PersistenceContext; 66 import org.objectstyle.cayenne.client.ClientEntityResolver; 67 import org.objectstyle.cayenne.distribution.BootstrapMessage; 68 import org.objectstyle.cayenne.distribution.ClientMessageHandler; 69 import org.objectstyle.cayenne.distribution.CommitMessage; 70 import org.objectstyle.cayenne.distribution.GenericQueryMessage; 71 import org.objectstyle.cayenne.distribution.SelectMessage; 72 import org.objectstyle.cayenne.distribution.UpdateMessage; 73 import org.objectstyle.cayenne.graph.GraphDiff; 74 import org.objectstyle.cayenne.map.EntityResolver; 75 76 83 public class ServerObjectContext extends ObjectDataContext implements 84 ClientMessageHandler { 85 86 public ServerObjectContext(DataDomain parentDomain) { 87 super(parentDomain); 88 } 89 90 public ServerObjectContext(PersistenceContext parent, EntityResolver entityResolver, 91 DataRowStore cache) { 92 93 super(parent, entityResolver, cache); 94 } 95 96 public GraphDiff onCommit(CommitMessage message) { 97 message.getSenderChanges().apply(new ClientToServerDiffConverter(this)); 99 100 GraphDiff diff = commit(); 101 102 if (diff.isNoop()) { 103 return diff; 104 } 105 else { 106 ServerToClientDiffConverter clientConverter = new ServerToClientDiffConverter( 108 getEntityResolver()); 109 diff.apply(clientConverter); 110 return clientConverter.getClientDiff(); 111 } 112 } 113 114 public QueryResponse onGenericQuery(GenericQueryMessage message) { 115 return performGenericQuery(message.getQueryPlan()); 116 } 117 118 public List onSelectQuery(SelectMessage message) { 119 List objects = performSelectQuery(message.getQueryPlan()); 120 121 123 if (objects.isEmpty()) { 124 return new ArrayList (0); 125 } 126 127 try { 128 return ClientServerUtils.toClientObjects(getEntityResolver(), objects); 129 } 130 catch (Exception e) { 131 throw new CayenneRuntimeException("Error converting to client objects: " 132 + e.getLocalizedMessage(), e); 133 } 134 } 135 136 public int[] onUpdateQuery(UpdateMessage message) { 137 return performUpdateQuery(message.getQueryPlan()); 138 } 139 140 public ClientEntityResolver onBootstrap(BootstrapMessage message) { 141 return entityResolver.getClientEntityResolver(); 142 } 143 } | Popular Tags |