View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Michal Bocek
40   * @since 1.0.0
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  	/* (non-Javadoc)
52  	 * @see com.vaadin.ui.FormFieldFactory#createField(com.vaadin.data.Item, java.lang.Object, com.vaadin.ui.Component)
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 }