1 package fr.ifremer.tutti.ui.swing.content.home.actions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.Sets;
27 import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
28 import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
29 import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocols;
30 import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
31 import fr.ifremer.tutti.ui.swing.TuttiScreen;
32 import fr.ifremer.tutti.ui.swing.content.actions.AbstractChangeScreenAction;
33 import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
34 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
35 import jaxx.runtime.context.JAXXContextEntryDef;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import javax.swing.JOptionPane;
40 import javax.swing.UIManager;
41 import java.util.Set;
42
43 import static org.nuiton.i18n.I18n.t;
44
45
46
47
48
49
50
51 public class EditProtocolAction extends AbstractChangeScreenAction {
52
53
54 private static final Log log = LogFactory.getLog(EditProtocolAction.class);
55
56 public static final JAXXContextEntryDef<TuttiProtocol> CLEAN_PROTOCOL_ENTRY = new JAXXContextEntryDef<>("cleanProtocol", TuttiProtocol.class);
57
58 public EditProtocolAction(MainUIHandler handler) {
59 super(handler, true, TuttiScreen.EDIT_PROTOCOL);
60 }
61
62 @Override
63 public boolean prepareAction() throws Exception {
64
65 CLEAN_PROTOCOL_ENTRY.removeContextValue(getContext().getMainUI());
66
67 boolean doAction = super.prepareAction();
68 if (doAction) {
69
70
71 SampleCategoryModel sampleCategoryModel =
72 getDataContext().getSampleCategoryModel();
73
74 TuttiProtocol protocol = getDataContext().getProtocol();
75
76 Set<Integer> badCategories = Sets.newHashSet();
77
78 TuttiProtocols.checkSampleCategories(sampleCategoryModel,
79 protocol,
80 badCategories);
81
82 if (!badCategories.isEmpty()) {
83
84
85 if (log.isWarnEnabled()) {
86 log.warn("There is some bad categories: " + badCategories);
87 }
88
89 String message = TuttiProtocols.getBadCategoriesMessage(
90 badCategories,
91 getDecorator(Caracteristic.class, null),
92 getContext().getPersistenceService());
93
94 String htmlMessage = String.format(
95 AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
96 message,
97 t("tutti.common.askBeforeEditProtocol.help"));
98 int response = JOptionPane.showOptionDialog(
99 getContext().getActionUI(),
100 htmlMessage,
101 t("tutti.common.askBeforeEditProtocol.title"),
102 JOptionPane.YES_NO_OPTION,
103 JOptionPane.WARNING_MESSAGE,
104 UIManager.getIcon("warning"),
105 new Object[]{t("tutti.option.cleanAndEdit"), t("tutti.option.edit"), t("tutti.option.cancel")},
106 t("tutti.option.cancel")
107 );
108
109 switch (response) {
110 case 0:
111
112 if (log.isInfoEnabled()) {
113 log.info("Clean and edit");
114 }
115
116 protocol = getContext().getPersistenceService().getProtocol(getContext().getProtocolId());
117
118 CLEAN_PROTOCOL_ENTRY.setContextValue(getContext().getMainUI(), protocol);
119
120 TuttiProtocols.removeBadCategories(sampleCategoryModel,
121 protocol);
122
123 break;
124
125 case 1:
126
127 if (log.isInfoEnabled()) {
128 log.info("Edit with no cleaning");
129 }
130 break;
131
132 default:
133
134
135 doAction = false;
136 }
137
138 }
139 }
140 return doAction;
141 }
142
143 @Override
144 public void doAction() throws Exception {
145 Preconditions.checkState(getContext().isProtocolFilled());
146 if (log.isInfoEnabled()) {
147 log.info("Edit protocol: " + getContext().getProtocolId());
148 }
149 createProgressionModelIfRequired(4);
150 super.doAction();
151 }
152
153 }