Skip to content

Commit

Permalink
dynamic form first impelemtation.
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed Jan 17, 2017
1 parent 47f5f6a commit ca40c6e
Show file tree
Hide file tree
Showing 8 changed files with 421 additions and 2 deletions.
Expand Up @@ -27,6 +27,7 @@
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
Expand Down Expand Up @@ -150,7 +151,7 @@ public void setProperties(List<ItemWrapper> properties) {
public <IW extends ItemWrapper> IW findPropertyWrapper(QName name) {
Validate.notNull(name, "QName must not be null.");
for (ItemWrapper wrapper : getItems()) {
if (name.equals(wrapper.getItem().getElementName())) {
if (QNameUtil.match(name, wrapper.getItem().getElementName())) {
return (IW) wrapper;
}
}
Expand Down
@@ -0,0 +1,27 @@
<!--
~ Copyright (c) 2010-2017 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<wicket:panel>
<div class="prism-header" wicket:id="header"/>
<div class="prism-properties">
<div>
<div class="row prism-property" wicket:id="property"/>
</div>
</div>
</wicket:panel>
</html>
@@ -0,0 +1,165 @@
package com.evolveum.midpoint.web.component.prism;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBElement;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;

import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinitionImpl;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.xml.XsdTypeMapper;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.form.Form;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AbstractFormItemType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FormDefinitionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FormFieldGroupType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FormFieldType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FormItemDisplayType;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;

public class DynamicFieldGroupPanel extends BasePanel{

private static final transient Trace LOGGER = TraceManager.getTrace(DynamicFieldGroupPanel.class);

private static final String ID_PROPERTY = "property";
private static final String ID_HEADER = "header";

private static final String ID_SUB_FIELD_GROUP = "subFieldGroup";

public DynamicFieldGroupPanel(String id, String groupName, ObjectWrapper objectWrapper, List<AbstractFormItemType> formItems, Form mainForm, PageBase parentPage) {
super(id);
setParent(parentPage);
initLayout(groupName, objectWrapper, formItems, mainForm);
}

public DynamicFieldGroupPanel(String id, ObjectWrapper objectWrapper, FormDefinitionType formDefinition, Form mainForm, PageBase parentPage) {
super(id);
setParent(parentPage);
String groupName = "Basic";
if (formDefinition.getDisplay() != null) {
groupName = formDefinition.getDisplay().getLabel();
}
initLayout(groupName, objectWrapper, getFormItems(formDefinition.getFormItem()), mainForm);
}

private void initLayout(String groupName, ObjectWrapper objectWrapper, List<AbstractFormItemType> formItems, Form mainForm) {

Label header = new Label(ID_HEADER, groupName);
add(header);

RepeatingView itemView = new RepeatingView(ID_PROPERTY);
add(itemView);

int i = 0;
for (AbstractFormItemType formItem : formItems) {

if (formItem instanceof FormFieldGroupType) {
DynamicFieldGroupPanel dynamicFieldGroupPanel = new DynamicFieldGroupPanel(itemView.newChildId(), formItem.getName(), objectWrapper, getFormItems(((FormFieldGroupType) formItem).getFormItem()), mainForm, getPageBase());
dynamicFieldGroupPanel.setOutputMarkupId(true);
itemView.add(dynamicFieldGroupPanel);
continue;
}

ItemWrapper itemWrapper = createItemWrapper(formItem, objectWrapper, !(i% 2 == 0));


applyFormDefinition(itemWrapper, formItem);

if (itemWrapper instanceof ContainerWrapper) {
PrismContainerPanel containerPanel = new PrismContainerPanel(itemView.newChildId(),
Model.of((ContainerWrapper)itemWrapper), true, mainForm, getPageBase());
containerPanel.setOutputMarkupId(true);
itemView.add(containerPanel);

} else {

PrismPropertyPanel propertyPanel = new PrismPropertyPanel(itemView.newChildId(),
Model.of(itemWrapper), mainForm, getPageBase());
propertyPanel.setOutputMarkupId(true);
itemView.add(propertyPanel);
}

i++;

}
}

private ItemWrapper createItemWrapper(AbstractFormItemType formField, ObjectWrapper objectWrapper, boolean isStriped) {
ItemPathType itemPathType = formField.getRef();

if (itemPathType == null) {
getSession().error("Bad form item definition. It has to contain reference to the real attribute");
LOGGER.error("Bad form item definition. It has to contain reference to the real attribute");
throw new RestartResponseException(getPageBase());
}

ItemPath path = itemPathType.getItemPath();

ItemWrapper itemWrapper = null;
Item item = objectWrapper.getObject().findItem(path);
if (item instanceof PrismContainer) {
itemWrapper = objectWrapper.findContainerWrapper(path);
} else {
itemWrapper = objectWrapper.findPropertyWrapper(path);
}

if (itemWrapper == null) {
getSession().error("Bad form item definition. No attribute with path: " + path + " was found");
LOGGER.error("Bad form item definition. No attribute with path: " + path + " was found");
throw new RestartResponseException(getPageBase());
}
itemWrapper.setStripe(isStriped);

return itemWrapper;

}

private void applyFormDefinition(ItemWrapper itemWrapper, AbstractFormItemType formField) {

FormItemDisplayType displayType = formField.getDisplay();

if (displayType == null) {
return;
}

ItemDefinitionImpl itemDef = (ItemDefinitionImpl) itemWrapper.getItemDefinition();
if (StringUtils.isNotEmpty(displayType.getLabel())) {
itemDef.setDisplayName(displayType.getLabel());
}

if (StringUtils.isNotEmpty(displayType.getHelp())) {
itemDef.setHelp(displayType.getHelp());
}

if (StringUtils.isNotEmpty(displayType.getMaxOccurs())) {
itemDef.setMaxOccurs(XsdTypeMapper.multiplicityToInteger(displayType.getMaxOccurs()));
}

if (StringUtils.isNotEmpty(displayType.getMinOccurs())) {
itemDef.setMinOccurs(XsdTypeMapper.multiplicityToInteger(displayType.getMinOccurs()));
}

}

private List<AbstractFormItemType> getFormItems(List<JAXBElement<? extends AbstractFormItemType>> formItems) {
List<AbstractFormItemType> items = new ArrayList<>();
for (JAXBElement<? extends AbstractFormItemType> formItem : formItems) {
AbstractFormItemType item = formItem.getValue();
items.add(item);
}
return items;
}


}
@@ -0,0 +1,22 @@
<!--
~ Copyright (c) 2010-2016 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<wicket:panel>
<div wicket:id="formFields"/>
</wicket:panel>
</html>

0 comments on commit ca40c6e

Please sign in to comment.