source: contrib/MailArchiver/sources/src/serpro/mailarchiver/view/admin/Search.java @ 6785

Revision 6785, 19.2 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
30package serpro.mailarchiver.view.admin;
31
32import java.text.DateFormat;
33import java.util.Date;
34import java.util.Locale;
35
36import org.springframework.beans.factory.annotation.Autowired;
37import org.springframework.beans.factory.annotation.Configurable;
38
39import static com.eventrouter.MessagePublisher.*;
40import com.eventrouter.annotation.Subscribe;
41
42import com.vaadin.data.util.BeanContainer;
43import com.vaadin.event.Action;
44import com.vaadin.event.ShortcutAction.KeyCode;
45import com.vaadin.ui.Button;
46import com.vaadin.ui.Button.ClickEvent;
47import com.vaadin.ui.HorizontalLayout;
48import com.vaadin.ui.Table;
49import com.vaadin.ui.TextField;
50import com.vaadin.ui.VerticalLayout;
51
52import serpro.mailarchiver.domain.metaarchive.AddressListField;
53import serpro.mailarchiver.domain.metaarchive.AddressListField_Address;
54import serpro.mailarchiver.domain.metaarchive.DateTimeField;
55import serpro.mailarchiver.domain.metaarchive.MailboxListField;
56import serpro.mailarchiver.domain.metaarchive.MailboxListField_Mailbox;
57import serpro.mailarchiver.domain.metaarchive.Message;
58import serpro.mailarchiver.domain.metaarchive.UnstructuredField;
59import serpro.mailarchiver.service.find.FMessage;
60import serpro.mailarchiver.util.Logger;
61import serpro.mailarchiver.util.transaction.WithReadOnlyTx;
62import serpro.mailarchiver.view.BaseComponent;
63
64class Search extends BaseComponent.VerticalLayout {
65
66    private static final Logger log = Logger.getLocalLogger();
67
68    Search() {
69        new SearchController(getDisplayId());
70    }
71
72    @Override
73    public final String getDisplayId() {
74        return "search";
75    }
76
77    private static final DateFormat dateFormat;
78    static {
79        Locale brazilLocale = Locale.forLanguageTag("pt-BR");
80        dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, brazilLocale);
81    }
82
83    @Autowired
84    private FMessage findMessage;
85
86    private final BeanContainer<String, MessageProxy> container = new BeanContainer<String, MessageProxy>(MessageProxy.class);
87
88    private Table table;
89    private Button search;
90
91    private final String PATH_PROPERTY = "path";
92    private final String SUBJECT_PROPERTY = "subject";
93    private final String DATE_PROPERTY = "date";
94    private final String SIZE_PROPERTY = "size";
95    private final String FROM_PROPERTY = "from";
96    private final String TO_PROPERTY = "to";
97    private final String CC_PROPERTY = "cc";
98    private final String BCC_PROPERTY = "bcc";
99    private final String OID_PROPERTY = "oid";
100
101    private static final Action ACTION_OPEN = new Action("Open");
102
103    @Subscribe({"/mailarchiver/fill_search_results"})
104    private void fillSearchResults(String[] oids) {
105        container.removeAllItems();
106        for(String oid : oids) {
107            container.addItem(oid, new MessageProxy(oid));
108        }
109    }
110
111    @Override
112    public void setHeight(float height, int unit) {
113        search.setEnabled(height != 0);
114        super.setHeight(height, unit);
115    }
116
117    @Override
118    public Search init() {
119
120        HorizontalLayout hlo = new HorizontalLayout();
121        hlo.setMargin(true);
122
123        final TextField queryExpression = new TextField();
124        queryExpression.setWidth("600px");
125        queryExpression.setInputPrompt("");
126        queryExpression.setImmediate(true);
127        hlo.addComponent(queryExpression);
128
129        search = new Button("Search");
130        search.setClickShortcut(KeyCode.ENTER);
131        search.addStyleName("primary");
132        search.addListener(new Button.ClickListener() {
133            @Override
134            public void buttonClick(ClickEvent event) {
135                String queryExpr = (String) queryExpression.getValue();
136                if(!queryExpr.isEmpty()) {
137                    publish("/mailarchiver/action/do_search", queryExpression.getValue());
138                }
139            }
140        });
141        hlo.addComponent(search);
142
143        addComponent(hlo);
144
145        VerticalLayout vlo = new VerticalLayout();
146        vlo.setMargin(true);
147
148        table = new ResultsTable("Results:");
149
150        table.setWidth("100%");
151        table.setPageLength(20);
152        table.setImmediate(true);
153        table.setSelectable(true);
154        table.setMultiSelect(false);
155        table.setSortDisabled(true);
156        table.setColumnReorderingAllowed(true);
157        table.setColumnCollapsingAllowed(true);
158        table.setRowHeaderMode(Table.ROW_HEADER_MODE_ICON_ONLY);
159
160        table.setContainerDataSource(container);
161
162        fillSearchResults(new String[] {});
163
164        table.setVisibleColumns(new Object[] {
165            PATH_PROPERTY,
166            SUBJECT_PROPERTY,
167            DATE_PROPERTY,
168            FROM_PROPERTY,
169            TO_PROPERTY,
170            CC_PROPERTY,
171            BCC_PROPERTY,
172            SIZE_PROPERTY,
173            OID_PROPERTY
174        });
175
176        table.setColumnHeader(PATH_PROPERTY, "Pasta");
177        table.setColumnIcon(PATH_PROPERTY, null);
178        table.setColumnAlignment(PATH_PROPERTY, Table.ALIGN_LEFT);
179        table.setColumnExpandRatio(PATH_PROPERTY, 0.2f);
180        table.setColumnCollapsed(PATH_PROPERTY, false);
181
182        table.setColumnHeader(SUBJECT_PROPERTY, "Assunto");
183        table.setColumnIcon(SUBJECT_PROPERTY, null);
184        table.setColumnAlignment(SUBJECT_PROPERTY, Table.ALIGN_LEFT);
185        table.setColumnExpandRatio(SUBJECT_PROPERTY, 0.3f);
186        table.setColumnCollapsed(SUBJECT_PROPERTY, false);
187
188        table.setColumnHeader(DATE_PROPERTY, "Data");
189        table.setColumnIcon(DATE_PROPERTY, null);
190        table.setColumnAlignment(DATE_PROPERTY, Table.ALIGN_LEFT);
191        table.setColumnExpandRatio(DATE_PROPERTY, 0.1f);
192        table.setColumnCollapsed(DATE_PROPERTY, false);
193
194        table.setColumnHeader(SIZE_PROPERTY, "Tamanho");
195        table.setColumnIcon(SIZE_PROPERTY, null);
196        table.setColumnAlignment(SIZE_PROPERTY, Table.ALIGN_LEFT);
197        table.setColumnExpandRatio(SIZE_PROPERTY, 0.1f);
198        table.setColumnCollapsed(SIZE_PROPERTY, false);
199
200        table.setColumnHeader(FROM_PROPERTY, "De");
201        table.setColumnIcon(FROM_PROPERTY, null);
202        table.setColumnAlignment(FROM_PROPERTY, Table.ALIGN_LEFT);
203        table.setColumnExpandRatio(FROM_PROPERTY, 0.2f);
204        table.setColumnCollapsed(FROM_PROPERTY, false);
205
206        table.setColumnHeader(TO_PROPERTY, "Para");
207        table.setColumnIcon(TO_PROPERTY, null);
208        table.setColumnAlignment(TO_PROPERTY, Table.ALIGN_LEFT);
209        table.setColumnExpandRatio(TO_PROPERTY, 0.2f);
210        table.setColumnCollapsed(TO_PROPERTY, false);
211
212        table.setColumnHeader(CC_PROPERTY, "Cópia");
213        table.setColumnIcon(CC_PROPERTY, null);
214        table.setColumnAlignment(CC_PROPERTY, Table.ALIGN_LEFT);
215        table.setColumnExpandRatio(CC_PROPERTY, 0.2f);
216        table.setColumnCollapsed(CC_PROPERTY, false);
217
218        table.setColumnHeader(BCC_PROPERTY, "Cópia oculta");
219        table.setColumnIcon(BCC_PROPERTY, null);
220        table.setColumnAlignment(BCC_PROPERTY, Table.ALIGN_LEFT);
221        table.setColumnExpandRatio(BCC_PROPERTY, 0.2f);
222        table.setColumnCollapsed(BCC_PROPERTY, true);
223
224        table.setColumnHeader(OID_PROPERTY, "ID");
225        table.setColumnIcon(OID_PROPERTY, null);
226        table.setColumnAlignment(OID_PROPERTY, Table.ALIGN_LEFT);
227        table.setColumnExpandRatio(OID_PROPERTY, 0.2f);
228        table.setColumnCollapsed(OID_PROPERTY, true);
229
230/*
231        table.addListener(new Table.ColumnResizeListener() {
232            @Override
233            public void columnResize(ColumnResizeEvent event) {
234                String s = String.format("Results column resize: propertyId=%s currentWidth=%s creviousWidth=%s",
235                        event.getPropertyId(), event.getCurrentWidth(), event.getPreviousWidth());
236                System.out.println(s);
237            }
238        });
239
240        table.addListener(new Table.HeaderClickListener() {
241            @Override
242            public void headerClick(HeaderClickEvent event) {
243                String s = String.format("Results header click: propertyId=%s", event.getPropertyId());
244                System.out.println(s);
245            }
246        });
247
248        table.addListener(new ItemClickListener() {
249            @Override
250            public void itemClick(ItemClickEvent event) {
251                String s = String.format("Results item click: itemId=%s propertyId=%s item=%s",
252                        event.getItemId(), event.getPropertyId(), event.getItem());
253                System.out.println(s);
254            }
255        });
256
257        table.addListener(new Property.ValueChangeListener() {
258            @Override
259            public void valueChange(ValueChangeEvent event) {
260                String s = String.format("Results value change: propertyId=%s", event.getProperty());
261                System.out.println(s);
262            }
263        });
264*/
265
266        vlo.addComponent(table);
267        addComponent(vlo);
268
269        return this;
270    }
271
272    @Configurable
273    public class ResultsTable extends Table {
274
275        public ResultsTable(String caption) {
276            super(caption);
277        }
278
279        @WithReadOnlyTx
280        @Override
281        protected void refreshRenderedCells() {
282            super.refreshRenderedCells();
283        }
284    }
285
286    public class MessageProxy {
287
288        private String oid;
289
290        public MessageProxy(String oid) {
291            this.oid = oid;
292        }
293
294        //--------------------------------------------------------------------------
295        private boolean messageFetched;
296        private Message message;
297
298        private void fetchMessageIfNeeded() {
299            if(!messageFetched) {
300                message = findMessage.byId(oid);
301                if(message == null) {
302                    log.error("message not found: %s", oid);
303                }
304                messageFetched = true;
305            }
306        }
307
308        //--------------------------------------------------------------------------
309        public String getOid() {
310            return oid;
311        }
312
313        public void setOid(String oid) {
314            throw new UnsupportedOperationException();
315        }
316
317        //--------------------------------------------------------------------------
318        private boolean pathFetched;
319        private String path = "";
320
321        public String getPath() {
322            if(!pathFetched) {
323                fetchMessageIfNeeded();
324                if(message == null) {
325                    size = "[not found]";
326                }
327                else {
328                    path = message.getFolder().getRelativePath().toString().replace('\\', '/');
329                }
330                pathFetched = true;
331            }
332            return path;
333        }
334
335        public void setPath(String path) {
336            throw new UnsupportedOperationException();
337        }
338
339        //--------------------------------------------------------------------------
340        private boolean subjectFetched;
341        private String subject = "";
342
343        public String getSubject() {
344            if(!subjectFetched) {
345                fetchMessageIfNeeded();
346                if(message == null) {
347                    subject = "[not found]";
348                }
349                else {
350                    UnstructuredField subjectField = message.getSubjectField();
351                    if(subjectField != null) {
352                        subject = subjectField.getText();
353                    }
354                }
355                subjectFetched = true;
356            }
357            return subject;
358        }
359
360        public void setSubject(String subject) {
361            throw new UnsupportedOperationException();
362        }
363
364        //--------------------------------------------------------------------------
365        private boolean dateFetched;
366        private String date = "";
367
368        public String getDate() {
369            if(!dateFetched) {
370                fetchMessageIfNeeded();
371                if(message == null) {
372                    date = "[not found]";
373                }
374                else {
375                    DateTimeField dateField = message.getDateField();
376                    if(dateField != null) {
377                        Date dt = dateField.getDate();
378                        if(dt != null) {
379                            date = dateFormat.format(dt);
380                        }
381                    }
382                }
383                dateFetched = true;
384            }
385            return date;
386        }
387
388        public void setDate(String date) {
389            throw new UnsupportedOperationException();
390        }
391
392        //--------------------------------------------------------------------------
393        private boolean sizeFetched;
394        private String size = "";
395
396        public String getSize() {
397            if(!sizeFetched) {
398                fetchMessageIfNeeded();
399                if(message == null) {
400                    size = "[not found]";
401                }
402                else {
403                    size = Integer.toString(message.getSize());
404                }
405                sizeFetched = true;
406            }
407            return size;
408        }
409
410        public void setSize(String size) {
411            throw new UnsupportedOperationException();
412        }
413
414        //--------------------------------------------------------------------------
415        private boolean fromFetched;
416        private String from = "";
417
418        public String getFrom() {
419            if(!fromFetched) {
420                fetchMessageIfNeeded();
421                if(message == null) {
422                    from = "[not found]";
423                }
424                else {
425                    MailboxListField fromField = message.getFromField();
426                    if(fromField != null) {
427                        StringBuilder sb = new StringBuilder();
428                        boolean first = true;
429                        for(MailboxListField_Mailbox mailbox : fromField.getMailboxList()) {
430                            if(first) {
431                                first = false;
432                            }
433                            else {
434                                sb.append(", ");
435                            }
436                            sb.append(mailbox.toDisplayString());
437                        }
438                        from = sb.toString();
439                    }
440                }
441                fromFetched = true;
442            }
443            return from;
444        }
445
446        public void setFrom(String from) {
447            throw new UnsupportedOperationException();
448        }
449
450        //--------------------------------------------------------------------------
451        private boolean toFetched;
452        private String to = "";
453
454        public String getTo() {
455            if(!toFetched) {
456                fetchMessageIfNeeded();
457                if(message == null) {
458                    to = "[not found]";
459                }
460                else {
461                    AddressListField toField = message.getToField();
462                    if(toField != null) {
463                        StringBuilder sb = new StringBuilder();
464                        boolean first = true;
465                        for(AddressListField_Address address : toField.getAddressList()) {
466                            if(first) {
467                                first = false;
468                            }
469                            else {
470                                sb.append(", ");
471                            }
472                            sb.append(address.toDisplayString());
473                        }
474                        to = sb.toString();
475                    }
476                }
477                toFetched = true;
478            }
479            return to;
480        }
481
482        public void setTo(String to) {
483            throw new UnsupportedOperationException();
484        }
485
486        //--------------------------------------------------------------------------
487        private boolean ccFetched;
488        private String cc = "";
489
490        public String getCc() {
491            if(!ccFetched) {
492                fetchMessageIfNeeded();
493                if(message == null) {
494                    cc = "[not found]";
495                }
496                else {
497                    AddressListField ccField = message.getCcField();
498                    if(ccField != null) {
499                        StringBuilder sb = new StringBuilder();
500                        boolean first = true;
501                        for(AddressListField_Address address : ccField.getAddressList()) {
502                            if(first) {
503                                first = false;
504                            }
505                            else {
506                                sb.append(", ");
507                            }
508                            sb.append(address.toDisplayString());
509                        }
510                        cc = sb.toString();
511                    }
512                }
513                ccFetched = true;
514            }
515            return cc;
516        }
517
518        public void setCc(String cc) {
519            throw new UnsupportedOperationException();
520        }
521
522        //--------------------------------------------------------------------------
523        private boolean bccFetched;
524        private String bcc = "";
525
526        public String getBcc() {
527            if(!bccFetched) {
528                fetchMessageIfNeeded();
529                if(message == null) {
530                    bcc = "[not found]";
531                }
532                else {
533                    AddressListField bccField = message.getBccField();
534                    if(bccField != null) {
535                        StringBuilder sb = new StringBuilder();
536                        boolean first = true;
537                        for(AddressListField_Address address : bccField.getAddressList()) {
538                            if(first) {
539                                first = false;
540                            }
541                            else {
542                                sb.append(", ");
543                            }
544                            sb.append(address.toDisplayString());
545                        }
546                        bcc = sb.toString();
547                    }
548                }
549                bccFetched = true;
550            }
551            return bcc;
552        }
553
554        public void setBcc(String bcc) {
555            throw new UnsupportedOperationException();
556        }
557    }
558}
Note: See TracBrowser for help on using the repository browser.