1 package fr.ifremer.dali.ui.swing.content.observation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.dali.dto.DaliBeans;
27 import fr.ifremer.dali.dto.data.measurement.MeasurementDTO;
28 import fr.ifremer.dali.service.control.ControlRuleMessagesBean;
29 import fr.ifremer.dali.ui.swing.action.AbstractDaliSaveAction;
30 import fr.ifremer.dali.ui.swing.content.observation.operation.measurement.grouped.OperationMeasurementsGroupedRowModel;
31 import fr.ifremer.quadrige3.core.dao.technical.Dates;
32
33 import static org.nuiton.i18n.I18n.t;
34
35
36
37
38 public class SaveAction extends AbstractDaliSaveAction<ObservationUIModel, ObservationUI, ObservationUIHandler> {
39
40 private ControlRuleMessagesBean controlMessages;
41
42
43
44
45
46
47 public SaveAction(final ObservationUIHandler handler) {
48 super(handler, false);
49 }
50
51
52 @Override
53 public boolean prepareAction() throws Exception {
54 if (!super.prepareAction()) {
55 return false;
56 }
57
58
59 if (DaliBeans.isSurveyValidated(getModel())) {
60 getContext().getDialogHelper().showErrorDialog(t("dali.action.save.observation.alreadySynchronized"), t("dali.action.save.observation"));
61 return false;
62 }
63
64
65 if (getModel().getCampaign() != null) {
66 if (getModel().getDate().isBefore(Dates.convertToLocalDate(getModel().getCampaign().getStartDate(), getConfig().getDbTimezone()))
67 || (getModel().getCampaign().getEndDate() != null && getModel().getDate().isAfter(Dates.convertToLocalDate(getModel().getCampaign().getEndDate(), getConfig().getDbTimezone())))) {
68
69 getContext().getDialogHelper().showErrorDialog(
70 t("dali.action.save.observation.notInCampaign", DaliBeans.toString(getModel()), DaliBeans.toString(getModel().getCampaign())),
71 t("dali.action.save.observations"));
72 return false;
73 }
74 }
75
76
77 if (isSurveyHasEmptyTaxonMeasurements()) {
78 getContext().getDialogHelper().showErrorDialog(t("dali.action.save.observation.emptyTaxonMeasurement"), t("dali.action.save.observation"));
79 return false;
80 }
81
82
83
84
85
86
87
88
89 return true;
90 }
91
92 private boolean isSurveyHasEmptyTaxonMeasurements() {
93
94
95 for (OperationMeasurementsGroupedRowModel row : getModel().getOperationMeasurementsTabUIModel().getGroupedTableUIModel().getRows()) {
96 if (row.getTaxonGroup() != null || row.getTaxon() != null) {
97 boolean rowWithoutMeasurements = true;
98 for (MeasurementDTO measurement : row.getMeasurements()) {
99 if (!DaliBeans.isMeasurementEmpty(measurement)) {
100 rowWithoutMeasurements = false;
101 break;
102 }
103 }
104 if (rowWithoutMeasurements) return true;
105 }
106 }
107
108 return false;
109 }
110
111
112 @Override
113 public void doAction() throws Exception {
114
115
116 getUI().getSurveyDetailsTabUI().getHandler().saveActualModel();
117 getUI().getPhotosTabUI().getHandler().saveActualModel();
118
119
120 getUI().getOperationMeasurementsTabUI().getHandler().save();
121
122
123 getContext().getObservationService().saveSurvey(getModel());
124
125
126 getModel().getSurveyDetailsTabUIModel().getUngroupedTableUIModel().getRows()
127 .forEach(surveyMeasurementsUngroupedRowModel -> surveyMeasurementsUngroupedRowModel.setErrors(null));
128
129
130 controlMessages = getContext().getRulesControlService().controlSurvey(getModel(),
131 false ,
132 true );
133
134 }
135
136
137 @Override
138 public void postSuccessAction() {
139 super.postSuccessAction();
140
141 getModel().setModify(false);
142
143
144 showControlResult(controlMessages, false);
145
146 getHandler().refreshModels();
147
148 }
149
150 }