рефакторинг массовых удалений объектов
This commit is contained in:
@@ -151,6 +151,9 @@ public class DataSet<K, D extends DBObject> extends DataSetAnchestor {
|
||||
public boolean ShowDeleteObjectDialog(DBObject object) {
|
||||
return UI.Warning(getSingleDescription() + " " + object.getBDialogName() + " будет удален(а)");
|
||||
}
|
||||
public boolean ShowDeleteObjectsDialog(int toDeleteCount) {
|
||||
return UI.Warning(getPluralDescription() + " в количестве " + toDeleteCount + " будут удалены)");
|
||||
}
|
||||
public String QName() {
|
||||
return "\"" + Name + "\"";
|
||||
}
|
||||
@@ -205,13 +208,13 @@ public class DataSet<K, D extends DBObject> extends DataSetAnchestor {
|
||||
protected Comparator<D> getComparator() {
|
||||
return null;
|
||||
}
|
||||
public int getCheckedCount() {
|
||||
public int getSelectedCount() {
|
||||
return (int) Data.values().stream().filter(d -> d.isVisible() && d.isSelected()).count();
|
||||
}
|
||||
public Vector<D> getCheckedItems() {
|
||||
public Vector<D> getSelectedItems() {
|
||||
return Data.values().stream().filter(d -> d.isVisible() && d.isSelected()).collect(Collectors.toCollection(Vector::new));
|
||||
}
|
||||
public Vector<K> getCheckedKeys() {
|
||||
public Vector<K> getSelectedKeys() {
|
||||
return Data.values().stream().filter(DBObject::isSelected).map(d -> (K) d.getPK()).collect(Collectors.toCollection(Vector::new));
|
||||
}
|
||||
//--
|
||||
@@ -262,6 +265,14 @@ public class DataSet<K, D extends DBObject> extends DataSetAnchestor {
|
||||
public boolean CheckCurrent(TextLog log) {
|
||||
return MainModule_.instance.Check(log, CurrentName());
|
||||
}
|
||||
public boolean CheckSelectedOrCurrent(TextLog log) {
|
||||
if ((getSelectedCount() == 0) && (CurrentName()==null || (getCurrent()==null))) {
|
||||
log.Writeln_(getPluralDescription() + ":");
|
||||
log.Writeln_("Отсутствуют отмеченные объекты, или текущий объект!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public boolean hasCurrent() {
|
||||
return MainModule_.instance.get(CurrentName()) != null;
|
||||
}
|
||||
@@ -274,15 +285,24 @@ public class DataSet<K, D extends DBObject> extends DataSetAnchestor {
|
||||
public void setCurrent(D o) {
|
||||
MainModule_.instance.set(CurrentName(), o);
|
||||
}
|
||||
public Vector<D> getCheckedOrCurrent() {
|
||||
public Vector<D> getSelectedOrCurrent() {
|
||||
Vector<D> res = new Vector<>();
|
||||
if (getCheckedCount() > 0)
|
||||
res = getCheckedItems();
|
||||
if (getSelectedCount() > 0)
|
||||
res = getSelectedItems();
|
||||
else {
|
||||
if (CurrentName() != null) {
|
||||
if (getCurrent() != null) {
|
||||
res.add(getCurrent());
|
||||
}
|
||||
if ((CurrentName() != null) && (getCurrent() != null)) {
|
||||
res.add(getCurrent());
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
public Vector<K> getSelectedOrCurrentKeys(){
|
||||
Vector<K> res = new Vector<>();
|
||||
if (getSelectedCount() > 0)
|
||||
res = getSelectedKeys();
|
||||
else {
|
||||
if ((CurrentName() != null) && (getCurrent() != null)) {
|
||||
res.add((K) getCurrent().getPK());
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
||||
59
src/Common/Passes/DeleteObjectsPass.java
Normal file
59
src/Common/Passes/DeleteObjectsPass.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package Common.Passes;
|
||||
import Common.Database.Objects.DBObject;
|
||||
public abstract class DeleteObjectsPass<D extends DBObject> extends ObjectsPass<D>{
|
||||
public DeleteObjectsPass(Class<D> d_in) {
|
||||
super(d_in);
|
||||
}
|
||||
@Override
|
||||
public String getIconPath() {
|
||||
return "/Common/icons/Delete.png";
|
||||
}
|
||||
@Override
|
||||
protected boolean canStart(Object... args) throws Exception {
|
||||
target = getTable().getSelectedOrCurrent();
|
||||
return getTable().CheckCurrent(Log) && getTable().ShowDeleteObjectsDialog(target.size());
|
||||
}
|
||||
//Очищаем все связанные таблицы, чтобы не допустить перерисовки во время удаления объекта.
|
||||
@Override
|
||||
protected void showPreparation() throws Exception {
|
||||
getTable().ClearUI();
|
||||
for (Class dep : getTable().getFKDependencies().keySet()) {
|
||||
switch (getTable().getFKDependencies().get(dep).data) {
|
||||
case NONE:
|
||||
case DROP:
|
||||
break;
|
||||
case DELETE:
|
||||
getDb().tables.get(dep).ClearUI();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected void body() throws Exception {
|
||||
getDb().BeginTransaction();
|
||||
for (D d: target){
|
||||
getDb().Delete(d);
|
||||
for (Class dep : getTable().getFKDependencies().keySet()) {
|
||||
switch (getTable().getFKDependencies().get(dep).data) {
|
||||
case NONE:
|
||||
break;
|
||||
case DROP:
|
||||
getDb().DropByFK(d, dep);
|
||||
break;
|
||||
case DELETE:
|
||||
getDb().DeleteByFK(d, dep);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
getDb().Commit();
|
||||
}
|
||||
//тут именно на финише, чтобы в любом случае вся таблица всегда была видна.
|
||||
@Override
|
||||
protected void performFinish() throws Exception {
|
||||
getTable().ShowUI();
|
||||
for (Class dep : getTable().getFKDependencies().keySet()) {
|
||||
getDb().tables.get(dep).RefreshUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/Common/Passes/ObjectsPass.java
Normal file
24
src/Common/Passes/ObjectsPass.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package Common.Passes;
|
||||
import Common.Database.Database;
|
||||
import Common.Database.Objects.DBObject;
|
||||
import Common.Database.Tables.DBTable;
|
||||
import Common.MainModule_;
|
||||
|
||||
import java.util.Vector;
|
||||
//массовый проход с объектами. на данный момент удаление.
|
||||
public abstract class ObjectsPass<D extends DBObject> extends Pass<Vector<D>> {
|
||||
protected Class<D> d; //класс объектов.
|
||||
public ObjectsPass(Class<D> d_in) {
|
||||
d = d_in;
|
||||
}
|
||||
protected Database getDb() {
|
||||
return MainModule_.instance.getDb();
|
||||
}
|
||||
public DBTable getTable() {
|
||||
return getDb().tables.get(d);
|
||||
} //таблица в источнике данных
|
||||
@Override//sorted
|
||||
public String getButtonText() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user