1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.view.admin; |
---|
31 | |
---|
32 | import java.io.File; |
---|
33 | |
---|
34 | import org.springframework.beans.factory.annotation.Autowired; |
---|
35 | |
---|
36 | import org.vaadin.easyuploads.MultiFileUpload; |
---|
37 | import org.vaadin.easyuploads.UploadField; |
---|
38 | import org.vaadin.easyuploads.UploadField.FieldType; |
---|
39 | |
---|
40 | import static com.eventrouter.MessagePublisher.*; |
---|
41 | |
---|
42 | import com.vaadin.data.Property; |
---|
43 | import com.vaadin.ui.AbstractSelect; |
---|
44 | import com.vaadin.ui.Button; |
---|
45 | import com.vaadin.ui.Button.ClickEvent; |
---|
46 | import com.vaadin.ui.ComboBox; |
---|
47 | import com.vaadin.ui.Component; |
---|
48 | import com.vaadin.ui.Label; |
---|
49 | |
---|
50 | import serpro.mailarchiver.domain.metaarchive.Folder; |
---|
51 | import serpro.mailarchiver.service.find.FFolder; |
---|
52 | import serpro.mailarchiver.session.Session; |
---|
53 | import serpro.mailarchiver.util.transaction.WithReadOnlyTx; |
---|
54 | import serpro.mailarchiver.view.BaseComponent; |
---|
55 | import serpro.mailarchiver.util.Logger; |
---|
56 | |
---|
57 | class Upload extends BaseComponent.VerticalLayout { |
---|
58 | |
---|
59 | private static final Logger log = Logger.getLocalLogger(); |
---|
60 | |
---|
61 | Upload() { |
---|
62 | new UploadController(getDisplayId()); |
---|
63 | } |
---|
64 | |
---|
65 | @Override |
---|
66 | public final String getDisplayId() { |
---|
67 | return "upload"; |
---|
68 | } |
---|
69 | |
---|
70 | @Autowired |
---|
71 | private FFolder findFolder; |
---|
72 | |
---|
73 | private final static int MAX_DEPTH = 20; |
---|
74 | final ComboBox[] comboStack = new ComboBox[MAX_DEPTH]; |
---|
75 | private boolean changing; |
---|
76 | |
---|
77 | @WithReadOnlyTx |
---|
78 | private void refreshComboItems(int depth) { |
---|
79 | |
---|
80 | Session.setThreadSession(AdminConsoleApp.getInstance().getSession()); |
---|
81 | |
---|
82 | ComboBox cbox = comboStack[depth]; |
---|
83 | cbox.removeAllItems(); |
---|
84 | |
---|
85 | String folderId = (depth > 0) ? (String) comboStack[depth-1].getValue() : "home"; |
---|
86 | |
---|
87 | Folder folder = findFolder.byId(folderId); |
---|
88 | |
---|
89 | for(Folder child : folder.getChildren()) { |
---|
90 | cbox.addItem(child.getOid()); |
---|
91 | cbox.setItemCaption(child.getOid(), child.getName()); |
---|
92 | } |
---|
93 | |
---|
94 | cbox.setValue(null); |
---|
95 | cbox.setEnabled(true); |
---|
96 | cbox.setVisible(true); |
---|
97 | } |
---|
98 | |
---|
99 | @Override |
---|
100 | public Upload init() { |
---|
101 | |
---|
102 | for(int i = 0; i < MAX_DEPTH; i++) { |
---|
103 | final ComboBox cbox = new ComboBox(); |
---|
104 | comboStack[i] = cbox; |
---|
105 | cbox.setWidth("250px"); |
---|
106 | cbox.setData(i); |
---|
107 | cbox.setFilteringMode(AbstractSelect.Filtering.FILTERINGMODE_OFF); |
---|
108 | cbox.setImmediate(true); |
---|
109 | cbox.setEnabled(false); |
---|
110 | cbox.setVisible(false); |
---|
111 | |
---|
112 | cbox.addListener(new Property.ValueChangeListener() { |
---|
113 | @Override |
---|
114 | public void valueChange(Property.ValueChangeEvent valueChangeEvent) { |
---|
115 | Object o = valueChangeEvent.getProperty().getValue(); |
---|
116 | if(changing) { |
---|
117 | return; |
---|
118 | } |
---|
119 | changing = true; |
---|
120 | int i = ((Integer)cbox.getData()); |
---|
121 | if(o == null) { |
---|
122 | for(int j = i + 1; j < MAX_DEPTH; j++) { |
---|
123 | ComboBox cb = comboStack[j]; |
---|
124 | if(cb.isVisible()) { |
---|
125 | cb.removeAllItems(); |
---|
126 | cb.setEnabled(false); |
---|
127 | cb.setVisible(false); |
---|
128 | } |
---|
129 | } |
---|
130 | refreshComboItems(i); |
---|
131 | } |
---|
132 | else { |
---|
133 | for(int j = i + 2; j < MAX_DEPTH; j++) { |
---|
134 | ComboBox cb = comboStack[j]; |
---|
135 | if(cb.isVisible()) { |
---|
136 | cb.removeAllItems(); |
---|
137 | cb.setEnabled(false); |
---|
138 | cb.setVisible(false); |
---|
139 | } |
---|
140 | } |
---|
141 | refreshComboItems(i + 1); |
---|
142 | } |
---|
143 | changing = false; |
---|
144 | } |
---|
145 | }); |
---|
146 | |
---|
147 | addComponent(cbox); |
---|
148 | } |
---|
149 | |
---|
150 | refreshComboItems(0); |
---|
151 | |
---|
152 | addComponent(hr()); |
---|
153 | |
---|
154 | final UploadField msgUp = new UploadField(); |
---|
155 | msgUp.setFieldType(FieldType.BYTE_ARRAY); |
---|
156 | msgUp.setCaption("Selecione uma mensagem para carregar no meta arquivamento"); |
---|
157 | Button btnLoad = new Button("Carregar"); |
---|
158 | btnLoad.addListener(new Button.ClickListener() { |
---|
159 | @Override |
---|
160 | public void buttonClick(ClickEvent event) { |
---|
161 | publish("/mailarchiver/tests/upload", msgUp.getValue(), getSelectedFolderId()); |
---|
162 | } |
---|
163 | }); |
---|
164 | addComponent(msgUp); |
---|
165 | addComponent(btnLoad); |
---|
166 | |
---|
167 | addComponent(hr()); |
---|
168 | |
---|
169 | addComponent(new Label("Teste de multifile upload")); |
---|
170 | final MultiFileUpload multiFileUpload = new MultiFileUpload() { |
---|
171 | @Override |
---|
172 | protected void handleFile(File file, String filename, String mimeType, long length) { |
---|
173 | publish("/mailarchiver/tests/upload", file, filename, getSelectedFolderId()); |
---|
174 | } |
---|
175 | }; |
---|
176 | multiFileUpload.setCaption("MultiFile Upload"); |
---|
177 | addComponent(multiFileUpload); |
---|
178 | |
---|
179 | return this; |
---|
180 | } |
---|
181 | |
---|
182 | private String getSelectedFolderId() { |
---|
183 | for(int j = MAX_DEPTH - 1; j >= 0; j--) { |
---|
184 | ComboBox cb = comboStack[j]; |
---|
185 | if(cb.isVisible()) { |
---|
186 | Object o = cb.getValue(); |
---|
187 | if(o != null) { |
---|
188 | return (String) o; |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | return "home"; |
---|
194 | } |
---|
195 | |
---|
196 | private Component hr() { |
---|
197 | return new Label("<hr>", Label.CONTENT_XHTML); |
---|
198 | } |
---|
199 | |
---|
200 | } |
---|