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 CloneProtocolAction extends AbstractChangeScreenAction {
52
53
54 private static final Log log = LogFactory.getLog(CloneProtocolAction.class);
55
56 public static final JAXXContextEntryDef<TuttiProtocol> CLONE_PROTOCOL_ENTRY = new JAXXContextEntryDef<>("cloneProtocol", TuttiProtocol.class);
57
58 protected TuttiProtocol protocol;
59
60 public CloneProtocolAction(MainUIHandler handler) {
61 super(handler, true, TuttiScreen.EDIT_PROTOCOL);
62 }
63
64 @Override
65 public boolean prepareAction() throws Exception {
66
67 CLONE_PROTOCOL_ENTRY.removeContextValue(getContext().getMainUI());
68
69 boolean doAction = super.prepareAction();
70 if (doAction) {
71
72
73 SampleCategoryModel sampleCategoryModel =
74 getDataContext().getSampleCategoryModel();
75
76 if (log.isInfoEnabled()) {
77 log.info("Clone protocol: " + getContext().getProtocolId());
78 }
79
80
81 protocol = getContext().getPersistenceService().getProtocol(getContext().getProtocolId());
82
83
84 protocol.setId((String) null);
85
86 Set<Integer> badCategories = Sets.newHashSet();
87
88 TuttiProtocols.checkSampleCategories(sampleCategoryModel,
89 protocol,
90 badCategories);
91
92 if (!badCategories.isEmpty()) {
93
94
95 if (log.isWarnEnabled()) {
96 log.warn("There is some bad categories: " + badCategories);
97 }
98
99 String message = TuttiProtocols.getBadCategoriesMessage(
100 badCategories,
101 getDecorator(Caracteristic.class, null),
102 getContext().getPersistenceService());
103 String htmlMessage = String.format(
104 AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
105 message,
106 t("tutti.common.askBeforeCloneProtocol.help"));
107 int response = JOptionPane.showOptionDialog(
108 getContext().getActionUI(),
109 htmlMessage,
110 t("tutti.common.askBeforeEditProtocol.title"),
111 JOptionPane.YES_NO_OPTION,
112 JOptionPane.WARNING_MESSAGE,
113 UIManager.getIcon("warning"),
114 new Object[]{t("tutti.option.cleanAndClone"), t("tutti.option.clone"), t("tutti.option.cancel")},
115 t("tutti.option.cancel")
116 );
117
118 switch (response) {
119 case 0:
120
121 if (log.isInfoEnabled()) {
122 log.info("Clean and Clone");
123 }
124 TuttiProtocols.removeBadCategories(sampleCategoryModel,
125 protocol);
126 break;
127
128 case 1:
129
130 if (log.isInfoEnabled()) {
131 log.info("Clone with no cleaning");
132 }
133 break;
134 default:
135
136
137 doAction = false;
138 }
139 }
140 }
141 return doAction;
142 }
143
144 @Override
145 public void doAction() throws Exception {
146 Preconditions.checkNotNull(protocol);
147
148
149 CLONE_PROTOCOL_ENTRY.setContextValue(getContext().getMainUI(), protocol);
150 protocol = null;
151
152 createProgressionModelIfRequired(4);
153
154
155 getContext().setProtocolId(null);
156
157 super.doAction();
158 }
159 }