/** * Get the intermediate join table name for self model and associated model. * * @param baseObj * The baseObj currently want to persist or update. * @param associatedModel * The associated model of baseObj. * @return The intermediate join table name. */ private String getJoinTableName(LitePalSupport baseObj, LitePalSupport associatedModel) { return getIntermediateTableName(baseObj, associatedModel.getTableName()); }
/** * This add an empty set for {@link LitePalSupport#associatedModelsMapForJoinTable}. * Might use for updating intermediate join table. * * @param baseObj * The baseObj currently want to persist or update. * @param associationInfo * To get associated table name from. */ private void declareAssociations(LitePalSupport baseObj, AssociationsInfo associationInfo) { baseObj.addEmptyModelForJoinTable(getAssociatedTableName(associationInfo)); }
/** * If the associated model of self model is null, the FK value in database * should be cleared if it exists when updating. * * @param baseObj * The baseObj currently want to persist or update. * @param associationInfo * The associated info analyzed by * {@link LitePalBase#getAssociationInfo(String)}. */ protected void mightClearFKValue(LitePalSupport baseObj, AssociationsInfo associationInfo) { baseObj.addFKNameToClearSelf(getForeignKeyName(associationInfo)); }
/** * After the model is updated, do the extra work that need to do. * * @param baseObj * Current model that is updated. * @param supportedGenericFields * List of all supported generic fields. */ private void afterUpdate(LitePalSupport baseObj, List<Field> supportedGenericFields) throws InvocationTargetException, IllegalAccessException { updateGenericTables(baseObj, supportedGenericFields, baseObj.getBaseObjId()); updateAssociatedTableWithFK(baseObj); insertIntermediateJoinTableValue(baseObj, true); clearFKValueInAssociatedTable(baseObj); }
/** * Unused currently. */ @SuppressWarnings("unused") private int doUpdateAssociations(LitePalSupport baseObj, long id, ContentValues values) { int rowsAffected = 0; analyzeAssociations(baseObj); updateSelfTableForeignKey(baseObj, values); rowsAffected += updateAssociatedTableForeignKey(baseObj, id); return rowsAffected; }
/** * Clear all the data for storing associated models' data. */ void clearAssociatedData() { clearIdOfModelWithFK(); clearIdOfModelWithoutFK(); clearIdOfModelForJoinTable(); clearFKNameList(); }
/** * First of all the associated model need to be saved already, or nothing * will be executed below. Then add the id of associated model into * {@link LitePalSupport#associatedModelsMapForJoinTable} for * inserting value into intermediate join table after baseObj is saved. * * @param baseObj * The baseObj currently want to persist or update. * @param associatedModel * The associated model of baseObj. */ private void dealAssociatedModel(LitePalSupport baseObj, LitePalSupport associatedModel) { if (associatedModel.isSaved()) { baseObj.addAssociatedModelForJoinTable(associatedModel.getTableName(), associatedModel.getBaseObjId()); } }
/** * Unused currently. */ private void updateSelfTableForeignKey(LitePalSupport baseObj, ContentValues values) { Map<String, Long> associatedModelMap = baseObj.getAssociatedModelsMapWithoutFK(); for (String associatedTable : associatedModelMap.keySet()) { String fkName = getForeignKeyColumnName(associatedTable); values.put(fkName, associatedModelMap.get(associatedTable)); } }
/** * Get the where clause to delete intermediate join table's value for * updating. * * @param baseObj * Current model that is persisted. * @return The where clause to execute. */ private String getWhereForJoinTableToDelete(LitePalSupport baseObj) { StringBuilder where = new StringBuilder(); where.append(getForeignKeyColumnName(baseObj.getTableName())); where.append(" = ?"); return where.toString(); }
/** * Deals unidirectional associations condition. * * @param baseObj * The baseObj currently want to persist. * @param associatedModel * The associated model of baseObj. */ private void unidirectionalCondition(LitePalSupport baseObj, LitePalSupport associatedModel) { dealsAssociationsOnTheSideWithoutFK(baseObj, associatedModel); }
/** * Deals with associated model on one side. * * @param baseObj * The baseObj currently want to persist. * @param associatedModel * The associated info analyzed by * {@link LitePalBase#getAssociationInfo(String)}. */ private void dealAssociatedModelOnOneSide(LitePalSupport baseObj, LitePalSupport associatedModel) { dealsAssociationsOnTheSideWithoutFK(baseObj, associatedModel); } }
/** * Get the foreign key associations of the specified class. * * @param className * The full class name. * @param isEager * True to load the associated models, false not. * @return The foreign key associations of the specified class */ protected List<AssociationsInfo> getForeignKeyAssociations(String className, boolean isEager) { if (isEager) { analyzeAssociations(className); return fkInCurrentModel; } return null; }
/** * Provide a way to mark all models in collection as deleted. This means these models' save * state is no longer exist anymore. If save them again, they will be treated as inserting new * data instead of updating the exist one. * @param collection * Collection of models which want to mark as deleted and clear their save state. */ public static <T extends LitePalSupport> void markAsDeleted(Collection<T> collection) { for (T t : collection) { t.clearSavedState(); } }
/** * Assign the generated id value to {@link LitePalSupport#baseObjId}. This * value will be used as identify of this model for system use. * * @param baseObj * The class of base object. * @param id * The value of id. */ protected void giveBaseObjIdValue(LitePalSupport baseObj, long id) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { if (id > 0) { DynamicExecutor.set(baseObj, "baseObjId", id, LitePalSupport.class); } }
/** * Clear all the data in {@link #associatedModelsMapWithoutFK}. */ private void clearIdOfModelWithoutFK() { getAssociatedModelsMapWithoutFK().clear(); }
@Override public void onClick(View v) { DataSupport.deleteAll(Book.class, "price < ?", "15"); } });