001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.lifecycle.initialize;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.kuali.rice.krad.uif.component.Component;
022import org.kuali.rice.krad.uif.container.Container;
023import org.kuali.rice.krad.uif.field.InputField;
024import org.kuali.rice.krad.uif.field.RemoteFieldsHolder;
025import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
026import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
027import org.kuali.rice.krad.uif.view.ViewModel;
028
029/**
030 * Process any remote fields holder that might be in the containers items.
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class ProcessRemoteFieldsHolderTask extends ViewLifecycleTaskBase<Container> {
035
036    /**
037     * Default constructor.
038     */
039    public ProcessRemoteFieldsHolderTask() {
040        super(Container.class);
041    }
042
043    /**
044     * Invoke custom initialization based on the view helper.
045     * 
046     * {@inheritDoc}
047     */
048    @Override
049    protected void performLifecycleTask() {
050        Container container = (Container) getElementState().getElement();
051        
052        if (!container.isProcessRemoteFieldHolders()) {
053            return;
054        }
055        
056        List<Component> processedItems = new ArrayList<Component>();
057
058        // check for holders and invoke to retrieve the remotable fields and translate
059        // translated fields are placed into the container item list at the position of the holder
060        for (Component item : container.getItems()) {
061            if (item instanceof RemoteFieldsHolder) {
062                List<InputField> translatedFields = ((RemoteFieldsHolder) item)
063                        .fetchAndTranslateRemoteFields(container);
064                processedItems.addAll(translatedFields);
065            } else {
066                processedItems.add(item);
067            }
068        }
069
070        // updated container items
071        container.setItems(processedItems);
072
073        
074        // invoke hook point for adding components through code
075        ViewLifecycle.getHelper().addCustomContainerComponents((ViewModel)ViewLifecycle.getModel(),
076                (Container) getElementState().getElement());
077    }
078
079}