Skip to content

Commit

Permalink
Adding LookupPropertyModel for GUI components with values from lookup…
Browse files Browse the repository at this point in the history
… table - shows label, saves and edits key.
  • Loading branch information
erik committed Apr 8, 2015
1 parent ba48ab8 commit d6c373d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 7 deletions.
Expand Up @@ -33,6 +33,7 @@
import com.evolveum.midpoint.web.component.input.*;
import com.evolveum.midpoint.web.component.model.delta.DeltaDto;
import com.evolveum.midpoint.web.component.model.delta.ModificationsPanel;
import com.evolveum.midpoint.web.component.util.LookupPropertyModel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.PageBase;
import com.evolveum.midpoint.web.util.DateValidator;
Expand Down Expand Up @@ -210,7 +211,7 @@ private int countUsableValues(PropertyWrapper property) {
}

private List<ValueWrapper> getUsableValues(PropertyWrapper property) {
List<ValueWrapper> values = new ArrayList<ValueWrapper>();
List<ValueWrapper> values = new ArrayList<>();
for (ValueWrapper value : property.getValues()) {
value.normalize();
if (ValueStatus.DELETED.equals(value.getStatus())) {
Expand Down Expand Up @@ -412,14 +413,13 @@ public String createAssociationTooltip(){
String lookupTableUid = valueEnumerationRef.getOid();
OperationResult result = new OperationResult("loadLookupTable");

// GetOperationOptions retrieveOption = GetOperationOptions.createRetrieve(RetrieveOption.INCLUDE);
// Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(retrieveOption);
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(LookupTableType.F_ROW,
GetOperationOptions.createRetrieve(RetrieveOption.INCLUDE));
final PrismObject<LookupTableType> lookupTable = WebModelUtils.loadObject(LookupTableType.class,
lookupTableUid, options, result, pageBase);

inputPanel = new AutoCompleteTextPanel<String>(id, new PropertyModel<String>(model, baseExpression + ".orig"), String.class) {
inputPanel = new AutoCompleteTextPanel<String>(id, new LookupPropertyModel<String>(model, baseExpression + ".orig",
lookupTable.asObjectable()), String.class) {

@Override
public Iterator<String> getIterator(String input) {
Expand Down Expand Up @@ -503,14 +503,12 @@ public DeltaDto getObject() {
String lookupTableUid = valueEnumerationRef.getOid();
OperationResult result = new OperationResult("loadLookupTable");

// GetOperationOptions retrieveOption = GetOperationOptions.createRetrieve(RetrieveOption.INCLUDE);
// Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(retrieveOption);
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(LookupTableType.F_ROW,
GetOperationOptions.createRetrieve(RetrieveOption.INCLUDE));
final PrismObject<LookupTableType> lookupTable = WebModelUtils.loadObject(LookupTableType.class,
lookupTableUid, options, result, pageBase);

panel = new AutoCompleteTextPanel<String>(id, new PropertyModel<String>(model, baseExpression), type) {
panel = new AutoCompleteTextPanel<String>(id, new LookupPropertyModel<String>(model, baseExpression, lookupTable.asObjectable()), type) {

@Override
public Iterator<String> getIterator(String input) {
Expand Down
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2010-2013 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.
*/

package com.evolveum.midpoint.web.component.util;

import com.evolveum.midpoint.web.util.WebMiscUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType;
import org.apache.wicket.Application;
import org.apache.wicket.Session;
import org.apache.wicket.core.util.lang.PropertyResolver;
import org.apache.wicket.core.util.lang.PropertyResolverConverter;
import org.apache.wicket.model.AbstractPropertyModel;

/**
* @author shood
* */
public class LookupPropertyModel<T> extends AbstractPropertyModel<T> {

private final String expression;
private LookupTableType lookupTable;

public LookupPropertyModel(Object modelObject, String expression, LookupTableType lookupTable){
super(modelObject);
this.expression = expression;
this.lookupTable = lookupTable;
}

/**
* @see org.apache.wicket.model.AbstractPropertyModel#propertyExpression()
*/
@Override
protected String propertyExpression(){
return expression;
}

@Override
@SuppressWarnings("unchecked")
public T getObject() {

final Object target = getInnermostModelOrObject();

if (target != null){
String key = (String)PropertyResolver.getValue(expression, target);

if(key == null){
return null;
}

for(LookupTableRowType row: lookupTable.getRow()){
if(key.equals(row.getKey())){
return (T)WebMiscUtil.getOrigStringFromPoly(row.getLabel());
}
}
}

return null;
}

@Override
public void setObject(T object) {
final String expression = propertyExpression();

PropertyResolverConverter prc;
prc = new PropertyResolverConverter(Application.get().getConverterLocator(),
Session.get().getLocale());

if(object instanceof String){
String label = (String) object;
String key;

for(LookupTableRowType row: lookupTable.getRow()){
if(label.equals(WebMiscUtil.getOrigStringFromPoly(row.getLabel()))){
key = row.getKey();

PropertyResolver.setValue(expression, getInnermostModelOrObject(), key, prc);
}
}
}
}

@Override
public void detach() {}
}

0 comments on commit d6c373d

Please sign in to comment.