|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2016 Development Gateway, Inc and others. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the MIT License (MIT) |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://opensource.org/licenses/MIT |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Development Gateway - initial API and implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package org.devgateway.ocds.forms.wicket.page.edit; |
| 13 | + |
| 14 | +import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation; |
| 15 | +import org.apache.wicket.markup.html.form.Form; |
| 16 | +import org.apache.wicket.markup.html.form.FormComponent; |
| 17 | +import org.apache.wicket.markup.html.form.validation.AbstractFormValidator; |
| 18 | +import org.apache.wicket.model.IModel; |
| 19 | +import org.apache.wicket.request.mapper.parameter.PageParameters; |
| 20 | +import org.apache.wicket.spring.injection.annot.SpringBean; |
| 21 | +import org.devgateway.ocds.forms.wicket.page.list.ListAllColorIndicatorPage; |
| 22 | +import org.devgateway.ocds.persistence.dao.ColorIndicatorPair; |
| 23 | +import org.devgateway.ocds.persistence.mongo.flags.FlagsConstants; |
| 24 | +import org.devgateway.ocds.persistence.repository.ColorIndicatorPairRepository; |
| 25 | +import org.devgateway.toolkit.forms.wicket.components.form.ColorPickerBootstrapFormComponent; |
| 26 | +import org.devgateway.toolkit.forms.wicket.components.form.Select2ChoiceBootstrapFormComponent; |
| 27 | +import org.devgateway.toolkit.forms.wicket.page.edit.AbstractEditPage; |
| 28 | +import org.devgateway.toolkit.forms.wicket.providers.GenericChoiceProvider; |
| 29 | +import org.devgateway.toolkit.persistence.repository.PersonRepository; |
| 30 | +import org.devgateway.toolkit.web.security.SecurityConstants; |
| 31 | +import org.wicketstuff.annotation.mount.MountPath; |
| 32 | + |
| 33 | +@AuthorizeInstantiation(SecurityConstants.Roles.ROLE_ADMIN) |
| 34 | +@MountPath("/editColorIndicatorPairPage") |
| 35 | +public class EditColorIndicatorPairPage extends AbstractEditPage<ColorIndicatorPair> { |
| 36 | + |
| 37 | + private static final long serialVersionUID = -6069250112046118104L; |
| 38 | + |
| 39 | + @Override |
| 40 | + protected ColorIndicatorPair newInstance() { |
| 41 | + return new ColorIndicatorPair(); |
| 42 | + } |
| 43 | + |
| 44 | + @SpringBean |
| 45 | + private ColorIndicatorPairRepository colorIndicatorPairRepository; |
| 46 | + |
| 47 | + @SpringBean |
| 48 | + private PersonRepository personRepository; |
| 49 | + |
| 50 | + private Select2ChoiceBootstrapFormComponent<String> firstIndicator; |
| 51 | + |
| 52 | + private Select2ChoiceBootstrapFormComponent<String> secondIndicator; |
| 53 | + |
| 54 | + public EditColorIndicatorPairPage(final PageParameters parameters) { |
| 55 | + super(parameters); |
| 56 | + this.jpaRepository = colorIndicatorPairRepository; |
| 57 | + this.listPageClass = ListAllColorIndicatorPage.class; |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void onInitialize() { |
| 64 | + super.onInitialize(); |
| 65 | + |
| 66 | + firstIndicator = |
| 67 | + new Select2ChoiceBootstrapFormComponent<String>("firstIndicator", |
| 68 | + new GenericChoiceProvider<String>(FlagsConstants.FLAGS_LIST)); |
| 69 | + firstIndicator.required(); |
| 70 | + editForm.add(firstIndicator); |
| 71 | + |
| 72 | + secondIndicator = |
| 73 | + new Select2ChoiceBootstrapFormComponent<String>("secondIndicator", |
| 74 | + new GenericChoiceProvider<String>(FlagsConstants.FLAGS_LIST)); |
| 75 | + secondIndicator.required(); |
| 76 | + editForm.add(secondIndicator); |
| 77 | + |
| 78 | + ColorPickerBootstrapFormComponent color = new ColorPickerBootstrapFormComponent("color"); |
| 79 | + color.required(); |
| 80 | + editForm.add(color); |
| 81 | + editForm.add(new ColorIndicatorDistinctFormValidator()); |
| 82 | + editForm.add(new ColorIndicatorUniquePairFormValidator(compoundModel)); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + private class ColorIndicatorDistinctFormValidator extends AbstractFormValidator { |
| 88 | + |
| 89 | + @Override |
| 90 | + public FormComponent<?>[] getDependentFormComponents() { |
| 91 | + return new FormComponent[]{firstIndicator.getField(), secondIndicator.getField()}; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void validate(Form<?> form) { |
| 96 | + if (firstIndicator.getField().getValue() != null && secondIndicator.getField().getValue() != null |
| 97 | + && firstIndicator.getField().getValue().equals(secondIndicator.getField().getValue())) { |
| 98 | + error(firstIndicator.getField()); |
| 99 | + error(secondIndicator.getField()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + private class ColorIndicatorUniquePairFormValidator extends AbstractFormValidator { |
| 106 | + |
| 107 | + private final IModel<ColorIndicatorPair> masterModel; |
| 108 | + |
| 109 | + ColorIndicatorUniquePairFormValidator(IModel<ColorIndicatorPair> masterModel) { |
| 110 | + this.masterModel = masterModel; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public FormComponent<?>[] getDependentFormComponents() { |
| 115 | + return new FormComponent[]{firstIndicator.getField(), secondIndicator.getField()}; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void validate(Form<?> form) { |
| 120 | + if (firstIndicator.getField().getValue() != null && secondIndicator.getField().getValue() != null) { |
| 121 | + ColorIndicatorPair indicator = colorIndicatorPairRepository. |
| 122 | + findByFirstIndicatorAndSecondIndicator(firstIndicator.getField().getValue(), |
| 123 | + secondIndicator.getField().getValue()); |
| 124 | + |
| 125 | + if ((masterModel.getObject().isNew() && indicator != null) |
| 126 | + || (!masterModel.getObject().isNew() && indicator != null |
| 127 | + && !indicator.getId().equals(masterModel.getObject().getId()))) { |
| 128 | + error(firstIndicator.getField()); |
| 129 | + error(secondIndicator.getField()); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments