1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.tapas.evidence.fe.child;
20
21 import java.util.List;
22 import java.util.Locale;
23
24 import lombok.extern.slf4j.Slf4j;
25
26 import org.vaadin.mvp.uibinder.IUiMessageSource;
27
28 import com.tapas.evidence.dto.ResponsiblePersonDTO;
29 import com.tapas.evidence.fe.form.EvidenceFormFieldFactory;
30 import com.tapas.evidence.service.ServiceHolder;
31 import com.vaadin.data.Item;
32 import com.vaadin.terminal.Sizeable;
33 import com.vaadin.ui.Component;
34 import com.vaadin.ui.DateField;
35 import com.vaadin.ui.Field;
36 import com.vaadin.ui.Select;
37
38
39
40
41
42 @Slf4j
43 public class ChildDetailFormFieldFactory extends EvidenceFormFieldFactory {
44
45 private static final long serialVersionUID = 1L;
46
47 public ChildDetailFormFieldFactory(final IUiMessageSource messageSource, final Locale locale) {
48 super(messageSource, locale);
49 }
50
51
52
53
54 @Override
55 public Field createField(final Item item, final Object propertyId, final Component uiContext) {
56 log.debug("Item: {}; Property id: {}; Componet: {}", new Object[] {item, propertyId, uiContext});
57 Field field = null;
58 final String pid = (String) propertyId;
59 if ("birthDate".equals(pid)) {
60 field = super.createField(item, propertyId, uiContext);
61 field.setWidth(100, Sizeable.UNITS_PERCENTAGE);
62 ((DateField)field).setDateFormat(this.getMessage("date.format"));
63 } else if ("motherId".equals(pid)) {
64 final Select select = new Select(pid);
65 Long kindergartenId = (Long)item.getItemProperty("kindergartenId").getValue();
66 reloadMothers(kindergartenId, select);
67 select.setNewItemsAllowed(false);
68 select.setNullSelectionAllowed(false);
69 select.setWidth(100, Sizeable.UNITS_PERCENTAGE);
70 field = select;
71 } else if ("fatherId".equals(pid)) {
72 final Select select = new Select(pid);
73 Long kindergartenId = (Long)item.getItemProperty("kindergartenId").getValue();
74 reloadFathers(kindergartenId, select);
75 select.setNewItemsAllowed(false);
76 select.setNullSelectionAllowed(false);
77 select.setWidth(100, Sizeable.UNITS_PERCENTAGE);
78 field = select;
79 } else if ("responsiblePersonId".equals(pid)) {
80 final Select select = new Select(pid);
81 Long kindergartenId = (Long)item.getItemProperty("kindergartenId").getValue();
82 reloadResponsiblePersons(kindergartenId, select);
83 select.setNewItemsAllowed(false);
84 select.setNullSelectionAllowed(false);
85 select.setWidth(100, Sizeable.UNITS_PERCENTAGE);
86 field = select;
87 } else if ("photo".equals(pid)) {
88 } else {
89 field = super.createField(item, propertyId, uiContext);
90 }
91 return field;
92 }
93
94 protected void reloadMothers(final Long kindergartenId, final Select mother) {
95 log.debug("reloading mothers for kindergatend {}", kindergartenId);
96 final List<ResponsiblePersonDTO> responsiblePersons = ServiceHolder.getInstance().getResponsibleService().findMothersByKindergartenId(kindergartenId);
97 for (ResponsiblePersonDTO responsiblePerson : responsiblePersons) {
98 if (!mother.getItemIds().contains(responsiblePerson.getId())) {
99 mother.addItem(responsiblePerson.getId());
100 mother.setItemCaption(responsiblePerson.getId(), responsiblePerson.getFullName());
101 }
102 }
103 }
104
105 protected void reloadFathers(final Long kindergartenId, final Select father) {
106 log.debug("reloading father for kindergatend {}", kindergartenId);
107 final List<ResponsiblePersonDTO> responsiblePersons = ServiceHolder.getInstance().getResponsibleService().findFathersByKindergartenId(kindergartenId);
108 for (ResponsiblePersonDTO responsiblePerson : responsiblePersons) {
109 if (!father.getItemIds().contains(responsiblePerson.getId())) {
110 father.addItem(responsiblePerson.getId());
111 father.setItemCaption(responsiblePerson.getId(), responsiblePerson.getFullName());
112 }
113 }
114 }
115
116 protected void reloadResponsiblePersons(final Long kindergartenId, final Select responsiblePersonSelect) {
117 log.debug("reloading responsible person for kindergatend {}", kindergartenId);
118 final List<ResponsiblePersonDTO> responsiblePersons = ServiceHolder.getInstance().getResponsibleService().findResponsiblePersonsByKindergartenId(kindergartenId);
119 for (ResponsiblePersonDTO responsiblePerson : responsiblePersons) {
120 if (!responsiblePersonSelect.getItemIds().contains(responsiblePerson.getId())) {
121 responsiblePersonSelect.addItem(responsiblePerson.getId());
122 responsiblePersonSelect.setItemCaption(responsiblePerson.getId(), responsiblePerson.getFullName());
123 }
124 }
125 }
126
127 }