View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.campaign.table;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.dali.dto.DaliBeans;
27  import fr.ifremer.dali.dto.data.survey.CampaignDTO;
28  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
29  import fr.ifremer.dali.ui.swing.util.DaliUIs;
30  import fr.ifremer.quadrige3.core.exception.DeleteForbiddenException;
31  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
32  import org.apache.commons.collections4.CollectionUtils;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.stream.Collectors;
37  
38  import static org.nuiton.i18n.I18n.t;
39  
40  /**
41   * Action permettant de supprimer une campagne.
42   */
43  public class DeleteCampaignAction extends AbstractDaliAction<CampaignsTableUIModel, CampaignsTableUI, CampaignsTableUIHandler> {
44  
45      private List<CampaignDTO> campaignsToDelete;
46      private boolean deleteAborted = false;
47  
48      /**
49       * Constructor.
50       *
51       * @param handler Le controleur
52       */
53      public DeleteCampaignAction(final CampaignsTableUIHandler handler) {
54          super(handler, false);
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public boolean prepareAction() throws Exception {
62          if (!super.prepareAction()) {
63              return false;
64          }
65  
66          if (getModel().getSelectedRows().isEmpty()) {
67              return false;
68          }
69  
70          campaignsToDelete = new ArrayList<>();
71          List<CampaignDTO> deniedCampaigns = new ArrayList<>();
72  
73          // Check already saved campaign
74          for (CampaignsTableRowModel campaign : getModel().getSelectedRows()) {
75              if (!campaign.isEditable())
76                  deniedCampaigns.add(campaign);
77  
78              if (campaign.getId() != null)
79                  campaignsToDelete.add(campaign);
80          }
81  
82          // Stop if a denied campaign is about to be delete
83          if (!deniedCampaigns.isEmpty()) {
84              getContext().getDialogHelper().showErrorDialog(
85                      t("dali.action.delete.campaign.denied.message"),
86                      DaliUIs.getHtmlString(deniedCampaigns.stream().map(CampaignDTO::getName).collect(Collectors.toList())),
87                      null,
88                      t("dali.action.delete.campaign.title")
89              );
90              return false;
91          }
92  
93          return askBeforeDelete(t("dali.action.delete.campaign.title"), t("dali.action.delete.campaign.message"));
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public void doAction() throws Exception {
101 
102         // check program usage in data
103         for (CampaignDTO campaign : campaignsToDelete) {
104 
105             long surveyCount = getContext().getObservationService().countSurveysWithCampaign(campaign.getId());
106             if (surveyCount > 0) {
107                 getContext().getDialogHelper().showErrorDialog(
108                         surveyCount == 1
109                                 ? t("dali.action.delete.campaign.used.data.message", campaign.getName())
110                                 : t("dali.action.delete.campaign.used.data.many.message", surveyCount, campaign.getName()),
111                         t("dali.action.delete.campaign.title"));
112                 deleteAborted = true;
113                 return;
114             }
115 
116             if (campaign.getId() != null) {
117                 if (getContext().getCampaignService().isCampaignUsedByFilter(campaign.getId())) {
118                     getContext().getDialogHelper().showErrorDialog(
119                             t("dali.action.delete.campaign.used.filter.message", campaign.getName()),
120                             t("dali.action.delete.campaign.title")
121                     );
122                     deleteAborted = true;
123                     return;
124                 }
125             }
126         }
127 
128         try {
129 
130             // Suppression des campagnes
131             getContext().getCampaignService().deleteCampaign(
132                     getContext().getAuthenticationInfo(),
133                     DaliBeans.collectIds(campaignsToDelete));
134 
135         } catch (DeleteForbiddenException e) {
136 
137             // Delete is forbidden (remote error), retrieve ids of campaigns
138             if (CollectionUtils.isNotEmpty(e.getObjectIds())) {
139                 List<CampaignDTO> campaigns = e.getObjectIds().stream()
140                         .map(objectId -> DaliBeans.findById(campaignsToDelete, Integer.parseInt(objectId)))
141                         .collect(Collectors.toList());
142                 getContext().getDialogHelper().showErrorDialog(
143                         t("dali.action.delete.campaign.used.data.remote.topMessage"),
144                         ApplicationUIUtil.getHtmlString(campaigns.stream().map(this::decorate).collect(Collectors.toList())),
145                         t("dali.action.delete.campaign.used.data.remote.bottomMessage"),
146                         t("dali.action.delete.campaign.title")
147                 );
148             } else {
149                 getContext().getDialogHelper().showErrorDialog(
150                         t("dali.action.delete.campaign.used.data.remote.message"),
151                         t("dali.action.delete.campaign.title")
152                 );
153             }
154             deleteAborted = true;
155         }
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public void postSuccessAction() {
163         super.postSuccessAction();
164 
165         // Suppression des lignes
166         if (!deleteAborted)
167             getModel().deleteSelectedRows();
168 
169     }
170 
171 }