use of org.apache.ofbiz.entity.GenericEntity in project ofbiz-framework by apache.
the class EntitySyncServices method runPullEntitySync.
/**
* Run Pull Entity Sync - Pull From Remote
*@param dctx The DispatchContext that this service is operating in
*@param context Map containing the input parameters
*@return Map with the result of the service, the output parameters
*/
public static Map<String, Object> runPullEntitySync(DispatchContext dctx, Map<String, Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
String entitySyncId = (String) context.get("entitySyncId");
String remotePullAndReportEntitySyncDataName = (String) context.get("remotePullAndReportEntitySyncDataName");
Debug.logInfo("Running runPullEntitySync for entitySyncId=" + context.get("entitySyncId"), module);
// loop until no data is returned to store
boolean gotMoreData = true;
Timestamp startDate = null;
Long toCreateInserted = null;
Long toCreateUpdated = null;
Long toCreateNotUpdated = null;
Long toStoreInserted = null;
Long toStoreUpdated = null;
Long toStoreNotUpdated = null;
Long toRemoveDeleted = null;
Long toRemoveAlreadyDeleted = null;
while (gotMoreData) {
gotMoreData = false;
// call pullAndReportEntitySyncData, initially with no results, then with results from last loop
Map<String, Object> remoteCallContext = new HashMap<String, Object>();
remoteCallContext.put("entitySyncId", entitySyncId);
remoteCallContext.put("delegatorName", context.get("remoteDelegatorName"));
remoteCallContext.put("userLogin", context.get("userLogin"));
remoteCallContext.put("startDate", startDate);
remoteCallContext.put("toCreateInserted", toCreateInserted);
remoteCallContext.put("toCreateUpdated", toCreateUpdated);
remoteCallContext.put("toCreateNotUpdated", toCreateNotUpdated);
remoteCallContext.put("toStoreInserted", toStoreInserted);
remoteCallContext.put("toStoreUpdated", toStoreUpdated);
remoteCallContext.put("toStoreNotUpdated", toStoreNotUpdated);
remoteCallContext.put("toRemoveDeleted", toRemoveDeleted);
remoteCallContext.put("toRemoveAlreadyDeleted", toRemoveAlreadyDeleted);
try {
Map<String, Object> result = dispatcher.runSync(remotePullAndReportEntitySyncDataName, remoteCallContext);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorCallingRemotePull", UtilMisc.toMap("remotePullAndReportEntitySyncDataName", remotePullAndReportEntitySyncDataName), locale), null, null, result);
}
startDate = (Timestamp) result.get("startDate");
try {
// anything to store locally?
if (startDate != null && (UtilValidate.isNotEmpty(result.get("valuesToCreate")) || UtilValidate.isNotEmpty(result.get("valuesToStore")) || UtilValidate.isNotEmpty(result.get("keysToRemove")))) {
// yep, we got more data
gotMoreData = true;
// at least one of the is not empty, make sure none of them are null now too...
List<GenericValue> valuesToCreate = checkList(result.get("valuesToCreate"), GenericValue.class);
if (valuesToCreate == null)
valuesToCreate = Collections.emptyList();
List<GenericValue> valuesToStore = checkList(result.get("valuesToStore"), GenericValue.class);
if (valuesToStore == null)
valuesToStore = Collections.emptyList();
List<GenericEntity> keysToRemove = checkList(result.get("keysToRemove"), GenericEntity.class);
if (keysToRemove == null)
keysToRemove = Collections.emptyList();
Map<String, Object> callLocalStoreContext = UtilMisc.toMap("entitySyncId", entitySyncId, "delegatorName", context.get("localDelegatorName"), "valuesToCreate", valuesToCreate, "valuesToStore", valuesToStore, "keysToRemove", keysToRemove);
callLocalStoreContext.put("userLogin", context.get("userLogin"));
Map<String, Object> storeResult = dispatcher.runSync("storeEntitySyncData", callLocalStoreContext);
if (ServiceUtil.isError(storeResult)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorCallingService", locale), null, null, storeResult);
}
// get results for next pass
toCreateInserted = (Long) storeResult.get("toCreateInserted");
toCreateUpdated = (Long) storeResult.get("toCreateUpdated");
toCreateNotUpdated = (Long) storeResult.get("toCreateNotUpdated");
toStoreInserted = (Long) storeResult.get("toStoreInserted");
toStoreUpdated = (Long) storeResult.get("toStoreUpdated");
toStoreNotUpdated = (Long) storeResult.get("toStoreNotUpdated");
toRemoveDeleted = (Long) storeResult.get("toRemoveDeleted");
toRemoveAlreadyDeleted = (Long) storeResult.get("toRemoveAlreadyDeleted");
}
} catch (GenericServiceException e) {
Debug.logError(e, "Error calling service to store data locally: " + e.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorCallingService", locale) + e.toString());
}
} catch (GenericServiceException e) {
Debug.logError(e, "Exception calling remote pull and report EntitySync service with name: " + remotePullAndReportEntitySyncDataName + "; " + e.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorCallingRemotePull", UtilMisc.toMap("remotePullAndReportEntitySyncDataName", remotePullAndReportEntitySyncDataName), locale) + e.toString());
} catch (Throwable t) {
Debug.logError(t, "Error calling remote pull and report EntitySync service with name: " + remotePullAndReportEntitySyncDataName + "; " + t.toString(), module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtErrorCallingRemotePull", UtilMisc.toMap("remotePullAndReportEntitySyncDataName", remotePullAndReportEntitySyncDataName), locale) + t.toString());
}
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.GenericEntity in project ofbiz-framework by apache.
the class FindByPrimaryKey method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
String entityName = entityNameFse.expandString(methodContext.getEnvMap());
boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap()));
Delegator delegator = getDelegator(methodContext);
Map<String, ? extends Object> inMap = mapFma.get(methodContext.getEnvMap());
if (inMap == null) {
throw new MiniLangRuntimeException("Primary key map \"" + mapFma + "\" not found", this);
}
if (entityName.isEmpty() && inMap instanceof GenericEntity) {
GenericEntity inEntity = (GenericEntity) inMap;
entityName = inEntity.getEntityName();
}
if (entityName.isEmpty()) {
throw new MiniLangRuntimeException("Entity name not found", this);
}
Collection<String> fieldsToSelectList = fieldsToSelectListFma.get(methodContext.getEnvMap());
try {
if (fieldsToSelectList != null) {
valueFma.put(methodContext.getEnvMap(), delegator.findByPrimaryKeyPartial(delegator.makePK(entityName, inMap), UtilMisc.toSet(fieldsToSelectList)));
} else {
valueFma.put(methodContext.getEnvMap(), EntityQuery.use(delegator).from(entityName).where(inMap).cache(useCache).queryOne());
}
} catch (GenericEntityException e) {
String errMsg = "Exception thrown while performing entity find: " + e.getMessage();
Debug.logWarning(e, errMsg, module);
simpleMethod.addErrorMessage(methodContext, errMsg);
return false;
}
return true;
}
use of org.apache.ofbiz.entity.GenericEntity in project ofbiz-framework by apache.
the class GenericDAO method singleUpdate.
private int singleUpdate(GenericEntity entity, ModelEntity modelEntity, List<ModelField> fieldsToSave, SQLProcessor sqlP) throws GenericEntityException {
if (modelEntity instanceof ModelViewEntity) {
return singleUpdateView(entity, (ModelViewEntity) modelEntity, fieldsToSave, sqlP);
}
// no non-primaryKey fields, update doesn't make sense, so don't do it
if (fieldsToSave.size() <= 0) {
if (Debug.verboseOn())
Debug.logVerbose("Trying to do an update on an entity with no non-PK fields, returning having done nothing; entity=" + entity, module);
// returning one because it was effectively updated, ie the same thing, so don't trigger any errors elsewhere
return 1;
}
if (modelEntity.lock()) {
GenericEntity entityCopy = GenericEntity.createGenericEntity(entity);
select(entityCopy, sqlP);
Object stampField = entity.get(ModelEntity.STAMP_FIELD);
if ((stampField != null) && (!stampField.equals(entityCopy.get(ModelEntity.STAMP_FIELD)))) {
String lockedTime = entityCopy.getTimestamp(ModelEntity.STAMP_FIELD).toString();
throw new EntityLockedException("You tried to update an old version of this data. Version locked: (" + lockedTime + ")");
}
}
// 2. don't set the stamp values if it is from an EntitySync (ie maintain original values), unless the stamps are null then set it anyway, ie even if it was from an EntitySync (also used for imports and such)
if (modelEntity.isField(ModelEntity.STAMP_TX_FIELD) && (!entity.getIsFromEntitySync() || entity.get(ModelEntity.STAMP_TX_FIELD) == null)) {
entity.set(ModelEntity.STAMP_TX_FIELD, TransactionUtil.getTransactionStartStamp());
addFieldIfMissing(fieldsToSave, ModelEntity.STAMP_TX_FIELD, modelEntity);
}
// if we have a STAMP_FIELD then update it with NOW.
if (modelEntity.isField(ModelEntity.STAMP_FIELD) && (!entity.getIsFromEntitySync() || entity.get(ModelEntity.STAMP_FIELD) == null)) {
entity.set(ModelEntity.STAMP_FIELD, TransactionUtil.getTransactionUniqueNowStamp());
addFieldIfMissing(fieldsToSave, ModelEntity.STAMP_FIELD, modelEntity);
}
StringBuilder sql = new StringBuilder().append("UPDATE ").append(modelEntity.getTableName(datasource)).append(" SET ");
modelEntity.colNameString(fieldsToSave, sql, "", "=?, ", "=?", false);
sql.append(" WHERE ");
SqlJdbcUtil.makeWhereStringFromFields(sql, modelEntity.getPkFieldsUnmodifiable(), entity, "AND");
int retVal = 0;
try {
sqlP.prepareStatement(sql.toString());
SqlJdbcUtil.setValues(sqlP, fieldsToSave, entity, modelFieldTypeReader);
SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
retVal = sqlP.executeUpdate();
entity.synchronizedWithDatasource();
} catch (GenericEntityException e) {
throw new GenericEntityException("Error while updating: " + entity.toString(), e);
}
if (retVal == 0) {
throw new GenericEntityNotFoundException("Tried to update an entity that does not exist, entity: " + entity.toString());
}
return retVal;
}
Aggregations