public static String save(EOEditingContext ec, boolean writeAnyWay, boolean merge) { if (log.isDebugEnabled()) log.debug("TolerantSaver: save..."); try {  ec.saveChanges(); } catch(NSValidation.ValidationException eov) { log.info("TolerantSaver: Caught EOValidationException: " + eov.getMessage()); } catch(EOGeneralAdaptorException e) { EOEnterpriseObject failedEO; NSDictionary userInfo = (NSDictionary)e.userInfo();      if(!(userInfo == null)) { String eType = (String)userInfo.objectForKey("EOAdaptorFailureKey"); if (!(eType == null)) { if (eType.equals("EOAdaptorOptimisticLockingFailure")) {  EOAdaptorOperation op = (EOAdaptorOperation) userInfo.objectForKey("EOFailedAdaptorOperationKey"); EODatabaseOperation dbop = (EODatabaseOperation) userInfo.objectForKey("EOFailedDatabaseOperationKey");  if (op != null && dbop != null) { NSDictionary changedValues =  op.changedValues();  NSDictionary snapshot = dbop.dbSnapshot(); if (log.isDebugEnabled()) log.debug("snapshot"+ snapshot); EOEntity ent = op.entity(); String entName = ent.name(); if (log.isDebugEnabled()) log.debug("entName"+ entName); NSArray pkAttribs = ent.primaryKeyAttributes(); EOQualifier qual = ERXTolerantSaver.qualifierWithSnapshotAndPks(pkAttribs, snapshot); EOFetchSpecification fs = new EOFetchSpecification(entName, qual, null); fs.setRefreshesRefetchedObjects(true); NSArray objs = ec.objectsWithFetchSpecification(fs); if (objs.count() > 0) { failedEO = (EOEnterpriseObject) objs.objectAtIndex(0); if (log.isDebugEnabled()) log.debug("failedEO"+ failedEO); if (merge) {        applyChangesToEO(changedValues, failedEO, ent); } } else { if (log.isDebugEnabled()) log.debug("TolerantSaver: EO was NOT there anymore!"); failedEO = null; } if (writeAnyWay) { if (log.isDebugEnabled()) log.debug("TolerantSaver: about to save changes again"); ec.saveChanges(); } return "EOAdaptorOptimisticLockingFailure"; } else { log.error("Missing EOFailedAdaptorOperationKey or EOFailedDatabaseOperationKey. "+e+"\n\n"+e.userInfo()); } } } else { log.error("TolerantSaver: No EOAdaptorFailureKey Exception:" + e); return "Error: No EOAdaptorFailureKey"; } } else { log.error("TolerantSaver: No UserInfo: "+e); return "Error: No UserInfo"; } } if (log.isDebugEnabled()) log.debug("TolerantSaver: save... done"); return null;
public static String save(EOEditingContext ec, boolean writeAnyWay) { return save(ec, writeAnyWay, true);
    public static String save(EOEditingContext ec, boolean writeAnyWay, boolean merge) {        if (log.isDebugEnabled()) log.debug("TolerantSaver: save...");        try {            //if (log.isDebugEnabled()) log.debug("about to save changes...");            ec.saveChanges();        } catch(NSValidation.ValidationException eov) {            log.info("TolerantSaver: Caught EOValidationException: " + eov.getMessage());        } catch(EOGeneralAdaptorException e) {            EOEnterpriseObject failedEO;            NSDictionary userInfo = (NSDictionary)e.userInfo();            //if (log.isDebugEnabled()) log.debug("TolerantSaver: Exception occurred name: "+ eName);            //if (log.isDebugEnabled()) log.debug("Exception occurred e: -------------------------");            //if (log.isDebugEnabled()) log.debug("Exception occurred e: "+e);            //if (log.isDebugEnabled()) log.debug("Exception occurred userInfo: "+ userInfo);            //if (log.isDebugEnabled()) log.debug("Exception occurred e: ^^^^^^^^^^^^^^^^^^^^^^^^^");            if(!(userInfo == null)) {                String eType = (String)userInfo.objectForKey("EOAdaptorFailureKey");                if (!(eType == null)) {                    if (eType.equals("EOAdaptorOptimisticLockingFailure")) {                        //if (log.isDebugEnabled()) log.debug("about to get EOFailedAdaptorOperationKey");                        EOAdaptorOperation op = (EOAdaptorOperation) userInfo.objectForKey("EOFailedAdaptorOperationKey");                        EODatabaseOperation dbop = (EODatabaseOperation) userInfo.objectForKey("EOFailedDatabaseOperationKey");                        //if (log.isDebugEnabled()) log.debug("about to get _changedValues");                        if (op != null && dbop != null) {                            NSDictionary changedValues =  op.changedValues();                            //if (log.isDebugEnabled()) log.debug("about to get _entity: _changedValues"+ changedValues);                            NSDictionary snapshot = dbop.dbSnapshot();                            if (log.isDebugEnabled()) log.debug("snapshot"+ snapshot);                            EOEntity ent = op.entity();                            String entName = ent.name();                            if (log.isDebugEnabled()) log.debug("entName"+ entName);                            NSArray pkAttribs = ent.primaryKeyAttributes();                            EOQualifier qual = ERXTolerantSaver.qualifierWithSnapshotAndPks(pkAttribs, snapshot);                            EOFetchSpecification fs = new EOFetchSpecification(entName, qual, null);                            fs.setRefreshesRefetchedObjects(true);                            NSArray objs = ec.objectsWithFetchSpecification(fs);                            if (objs.count() > 0) {                                failedEO = (EOEnterpriseObject) objs.objectAtIndex(0);                                if (log.isDebugEnabled()) log.debug("failedEO"+ failedEO);                                if (merge) {                                    //EODatabaseContext dbcontext = ChangeCatcher.databaseContextForEntityNamed (entName, ec);                                    //EODatabase db = dbcontext.database();                                    //EOGlobalID gid = ec.globalIDForObject(failedEO);                                    //NSMutableDictionary ss = new NSMutableDictionary(snapshot);                                    //ss. addEntriesFromDictionary(changedValues);                                    //db.forgetSnapshotForGlobalID(gid);                                    //db.recordSnapshotForGlobalID(ss, gid);                                    applyChangesToEO(changedValues, failedEO, ent);                                }                            } else {                                if (log.isDebugEnabled()) log.debug("TolerantSaver: EO was NOT there anymore!");                                failedEO = null;                            }                            if (writeAnyWay) {                                if (log.isDebugEnabled()) log.debug("TolerantSaver: about to save changes again");                                ec.saveChanges();                            }                            return "EOAdaptorOptimisticLockingFailure";                        } else {                            log.error("Missing EOFailedAdaptorOperationKey or EOFailedDatabaseOperationKey. "+e+"\n\n"+e.userInfo());                        }                    }                                    } else {                    log.error("TolerantSaver: No EOAdaptorFailureKey Exception:" + e);                    return "Error: No EOAdaptorFailureKey";                }            } else {                log.error("TolerantSaver: No UserInfo: "+e);                return "Error: No UserInfo";            }        }        if (log.isDebugEnabled()) log.debug("TolerantSaver: save... done");        return null;    }