/** * Clear associated models' save state. After this method, the associated models of baseObj which * data is removed from database will become unsaved. * * @param baseObj The record to delete. * @param associationInfos The associated info. */ private void clearAssociatedModelSaveState( DataSupport baseObj, Collection<AssociationsInfo> associationInfos) { try { for (AssociationsInfo associationInfo : associationInfos) { if (associationInfo.getAssociationType() == Const.Model.MANY_TO_ONE && !baseObj.getClassName().equals(associationInfo.getClassHoldsForeignKey())) { Collection<DataSupport> associatedModels = getAssociatedModels(baseObj, associationInfo); if (associatedModels != null && !associatedModels.isEmpty()) { for (DataSupport model : associatedModels) { if (model != null) { model.resetBaseObjId(); } } } } else if (associationInfo.getAssociationType() == Const.Model.ONE_TO_ONE) { DataSupport model = getAssociatedModel(baseObj, associationInfo); if (model != null) { model.resetBaseObjId(); } } } } catch (Exception e) { throw new DataSupportException(e.getMessage()); } }
/** * Package a {@link org.litepal.crud.model.AssociationsInfo}, and add it into {@link * #mAssociationInfos} Collection. * * @param selfClassName The class name of self model. * @param associatedClassName The class name of the class which associated with self class. * @param classHoldsForeignKey The class which holds foreign key. * @param associateOtherModelFromSelf The field of self class to declare has association with * other class. * @param associateSelfFromOtherModel The field of the associated class to declare has association * with self class. * @param associationType The association type. */ private void addIntoAssociationInfoCollection( String selfClassName, String associatedClassName, String classHoldsForeignKey, Field associateOtherModelFromSelf, Field associateSelfFromOtherModel, int associationType) { AssociationsInfo associationInfo = new AssociationsInfo(); associationInfo.setSelfClassName(selfClassName); associationInfo.setAssociatedClassName(associatedClassName); associationInfo.setClassHoldsForeignKey(classHoldsForeignKey); associationInfo.setAssociateOtherModelFromSelf(associateOtherModelFromSelf); associationInfo.setAssociateSelfFromOtherModel(associateSelfFromOtherModel); associationInfo.setAssociationType(associationType); mAssociationInfos.add(associationInfo); }
/** * Analyze the associations of modelClass and store the associated tables. The associated tables * might be used when deleting referenced data of a specified row. * * @param modelClass To get associations of this class. */ private void analyzeAssociations(Class<?> modelClass) { Collection<AssociationsInfo> associationInfos = getAssociationInfo(modelClass.getName()); for (AssociationsInfo associationInfo : associationInfos) { String associatedTableName = DBUtility.getTableNameByClassName(associationInfo.getAssociatedClassName()); if (associationInfo.getAssociationType() == Const.Model.MANY_TO_ONE || associationInfo.getAssociationType() == Const.Model.ONE_TO_ONE) { String classHoldsForeignKey = associationInfo.getClassHoldsForeignKey(); if (!modelClass.getName().equals(classHoldsForeignKey)) { getForeignKeyTableToDelete().add(associatedTableName); } } else if (associationInfo.getAssociationType() == Const.Model.MANY_TO_MANY) { String joinTableName = DBUtility.getIntermediateTableName(getTableName(modelClass), associatedTableName); joinTableName = BaseUtility.changeCase(joinTableName); getForeignKeyTableToDelete().add(joinTableName); } } }