/** * Modify in the Database the objects in the array received as parameter. * * @param An Object that implements java.util.Collection with the items * @return An ArrayList with the index of each item if collection if from a Reference Collection * or an ArrayList with the number of item that couldnt be updated if this is not a Collection * Reference */ public ArrayList<Integer> modifyAll(Collection array) { ArrayList<Integer> results = new ArrayList<Integer>(); try { boolean commitValue = this.commit; if (this.commit || this.autoCommit) { conex.initTransaction(); this.commit = false; } Object objects[] = array.toArray(); if (!this.collection) { for (int i = 0; i < objects.length; i++) { if (!this.modify(objects[i])) { results.add(i); break; } } } else { this.collection = false; for (Object obj : objects) { int index = this.modifyGetIndex(obj); results.add(index); } this.collection = true; } this.commit = commitValue; if (this.commit || this.autoCommit) conex.confirmTransaction(); } catch (SQLException e) { this.commit = true; } return results; }
/** * For this method only is the object to be modify. It is necessary to execute obtain previously * to recover the proper object. * * @param Object * @return True if the modification was successfull, False in the other case */ public boolean modify(Object object) { if (!this.manager.checkOptimisticLock(this, object)) { throw new OptimisticLockException(); } boolean value = false; try { boolean commitValue = this.commit; if (this.commit || this.autoCommit) { conex.initTransaction(); this.commit = false; } ArrayList<String> manys = this.manager.getRef().isMany2Many(object.getClass()); if (!manys.isEmpty()) { if (this.manager.deleteMany2Many(this, object, manys)) { this.manager.cleanStack(); value = this.save(object); } } else { value = this.modifyProcess(object); } this.commit = commitValue; if (this.commit || this.autoCommit) conex.confirmTransaction(); } catch (SQLException e) { this.commit = true; } return value; }
/** * Store the object in the Database (create the Table if not exist) and return the value of the * PrimaryKey. * * @param Object * @return The value of the PrimaryKey, or -1 if the object couldn't be saved. */ public int saveGetIndex(Object object) { int index = -1; try { boolean commitValue = this.commit; if (this.commit || this.autoCommit) { conex.initTransaction(); this.commit = false; } ArrayList array = this.manager.entity2Array(this, object, EntityManager.OPERATION.SAVE); index = this.saveProcess(array, object); this.commit = commitValue; if (this.commit || this.autoCommit) { conex.confirmTransaction(); if (this.conex.getConnection().getAutoCommit()) { this.conex.getConnection().setAutoCommit(false); } } } catch (SQLException e) { this.commit = true; } return index; }
/** * Delete the object received as parameter from the Database * * @param Object * @return True if the operation complete successfully, False in the other case. */ public boolean delete(Object object) { try { boolean commitValue = this.commit; if (this.commit || this.autoCommit) { conex.initTransaction(); this.commit = false; } ArrayList array = this.manager.entity2Array(this, object, EntityManager.OPERATION.DELETE); conex.deleteRows(((String) array.get(0)), ((String) array.get(array.size() - 1))); this.manager.deleteParent(this, object); // Obtain the attributes from this object that are collections // and delete their relations in the Relational Table. ArrayList<String[]> strings = this.manager.getCollectionsTableForDelete(object, this); int index = (Integer) ((Object[]) array.get(1))[1]; for (String[] s : strings) { if (this.checkTableExist(s[0])) { this.executeQuery("DELETE FROM " + s[0] + " WHERE base=" + index); } else if (this.checkTableExist(s[1])) { this.executeQuery("DELETE FROM " + s[1] + " WHERE related=" + index); } } this.commit = commitValue; if (this.commit || this.autoCommit) conex.confirmTransaction(); } catch (Exception e) { this.commit = true; try { conex.cancelTransaction(); } catch (Exception ex) { return false; } return false; } return true; }
/** * Save all the items in the Collection in the Database * * @param An Object that implement java.util.Collection * @return An ArrayList with the number of elements inserted or an ArrayList with each item index * if the array received represent a Collection Reference. */ public ArrayList<Integer> saveAll(Collection array) { ArrayList<Integer> results = new ArrayList<Integer>(); try { boolean commitValue = this.commit; if (this.commit || this.autoCommit) { conex.initTransaction(); this.commit = false; } Object objects[] = array.toArray(); if (!this.collection) { for (int i = 0; i < objects.length; i++) { if (!this.save(objects[i])) { results.add(i); // throw exception break; } } } else { this.collection = false; for (Object obj : objects) { results.add(this.saveGetIndex(obj)); } this.collection = true; } this.commit = commitValue; if (this.commit || this.autoCommit) { conex.confirmTransaction(); if (this.conex.getConnection().getAutoCommit()) { this.conex.getConnection().setAutoCommit(false); } } } catch (SQLException e) { this.commit = true; } return results; }