|
|
|
Re: Delete [message #748005 is a reply to message #746457] |
Mon, 24 October 2011 12:21 |
Jeremie Bresson Messages: 1252 Registered: October 2011 |
Senior Member |
|
|
Is your solution working?
Here my example (maybe you can find one or two ideas):
Entity are here Actors.
I add in each process service a delete method:
public class ActorProcessService extends AbstractService implements IActorProcessService {
// other methods
public boolean delete(Long[] actorIds) throws ProcessingException {
if (!ACCESS.check(new DeleteActorPermission())) {
throw new VetoException(Texts.get("AuthorizationFailed"));
}
int nbRows = SQL.delete(
" delete from actor" +
" where actor_id = :id",
new NVPair("id", actorIds)
);
return nbRows > 0;
}
}
In the TablePage representing this entity, I have this delete menu:
@Order(30.0)
public class DeleteMenu extends AbstractMenu {
@Override
protected String getConfiguredText() {
return ScoutTexts.get("DeleteMenu");
}
@Override
protected boolean getConfiguredMultiSelectionAction() {
return true;
}
@Override
protected void execAction() throws ProcessingException {
String[] actors = new String[getTable().getSelectedRowCount()];
String[] firstNames = getTable().getFirstNameColumn().getSelectedValues();
String[] lastNames = getTable().getLastNameColumn().getSelectedValues();
for (int i = 0; i < actors.length; i++) {
actors[i] = StringUtility.join(" ", firstNames[i], lastNames[i]);
}
if (MessageBox.showDeleteConfirmationMessage(Texts.get("Actor"), actors)) {
boolean result = SERVICES.getService(IActorProcessService.class).delete(getTable().getActorIdColumn().getSelectedValues());
if (result) {
reloadPage();
}
}
}
}
This is very similar to what you are doing.
In forms, if you have a TableField there is another possibility: keep trace of the removed rows in the FormData... and delete them in the server. For example, imagine you have a Form to edit Films, and the entity Film has a N::M relation to Actor. In the Film Form you might want to add and remove Actors.
A solution could be to display the Actors of a Film in a TableField in the Film Form. If the user remove an Actor for the film, you keep the information of the deleted actor in the FormData, and in the modify method of your Film ProcessService, you delete the entry in your film_actor table.
I can provide you an example of this second case if you are interested.
|
|
|
|
Re: Delete [message #750233 is a reply to message #748561] |
Tue, 25 October 2011 17:44 |
Jeremie Bresson Messages: 1252 Registered: October 2011 |
Senior Member |
|
|
Here the TableField in the MainBox of the Film Form:
@Order(120.0)
public class ActorsTableField extends AbstractTableField<ActorsTableField.Table> {
@Override
protected int getConfiguredGridH() {
return 6;
}
@Override
protected int getConfiguredGridW() {
return 2;
}
@Override
protected String getConfiguredLabel() {
return Texts.get("Actors");
}
@Order(10.0)
public class Table extends AbstractTable {
//Inner-classes defining columns ActorIdColumn, FirstNameColumn, LastNameColumn
@Order(10.0)
public class AddMenu extends AbstractMenu {
@Override
protected boolean getConfiguredEmptySpaceAction() {
return true;
}
@Override
protected boolean getConfiguredSingleSelectionAction() {
return false;
}
@Override
protected String getConfiguredText() {
return Texts.get("Add");
}
@Override
protected void execAction() throws ProcessingException {
//Logic to add an Actor:
//Open a new Form where it is possible to search and to select an actor.
//Add the actor with:
// addRowsByMatrix(new Object[]{new Object[]{
// actorId,
// firstName,
// lastName,
// }});
}
}
@Order(20.0)
public class DeleteMenu extends AbstractMenu {
@Override
protected String getConfiguredText() {
return Texts.get("Delete");
}
@Override
protected boolean getConfiguredMultiSelectionAction() {
return true;
}
@Override
protected void execAction() throws ProcessingException {
deleteRows(getSelectedRows());
}
}
}
}
I also have a Form Variable filmId in the form, containing the film id. Therefore the film id is contained in the formData:
In my process service, at the end of the create() and store() function, I call this function:
private void storeFilmActor(FilmFormData formData) throws ProcessingException {
ActorsTable table = formData.getActorsTable();
for (int i = 0; i < table.getRowCount(); i++) {
switch (table.getRowState(i)) {
case ITableHolder.STATUS_INSERTED:
SQL.insert(
" insert into film_actor(" +
" film_id, " +
" actor_id " +
" ) values( " +
" :FilmId, " +
" :ActorId " +
" ) ",
formData,
new NVPair("ActorId", table.getActorId(i))
);
break;
case ITableHolder.STATUS_DELETED:
SQL.delete(
" delete from film_actor " +
" where film_id = :FilmId " +
" and actor_id = :ActorId",
formData,
new NVPair("ActorId", table.getActorId(i))
);
break;
case ITableHolder.STATUS_NON_CHANGED:
case ITableHolder.STATUS_UPDATED:
default:
//Do nothing
break;
}
}
}
By the way you also might be interested in the code to populate the ActorTableField in the load() function of my process service:
SQL.selectInto(
"select actor_id, " +
" first_name, " +
" last_name " +
" from actor " +
" where 1 = 1 " +
" and actor_id in (select actor_id from film_actor where film_id = :FilmId) " +
" into :ActorId, " +
" :FirstName," +
" :LastName ",
formData,
formData.getActorsTable()
);
|
|
|
Powered by
FUDForum. Page generated in 0.04532 seconds