001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.krad.uif.container;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.rice.krad.uif.component.Component;
022    import org.kuali.rice.krad.uif.field.InputField;
023    import org.kuali.rice.krad.uif.field.RemoteFieldsHolder;
024    import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
025    import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
026    import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
027    import 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     */
034    public class ProcessRemoteFieldsHolderTask extends ViewLifecycleTaskBase<Container> {
035    
036        /**
037         * Constructor.
038         * 
039         * @param phase The initialize phase for this container.
040         */
041        public ProcessRemoteFieldsHolderTask(ViewLifecyclePhase phase) {
042            super(phase, Container.class);
043        }
044    
045        /**
046         * Invoke custom initialization based on the view helper.
047         * 
048         * {@inheritDoc}
049         */
050        @Override
051        protected void performLifecycleTask() {
052            Container container = (Container) getElementState().getElement();
053            
054            if (!container.isProcessRemoteFieldHolders()) {
055                return;
056            }
057            
058            List<Component> processedItems = new ArrayList<Component>();
059    
060            // check for holders and invoke to retrieve the remotable fields and translate
061            // translated fields are placed into the container item list at the position of the holder
062            for (Component item : container.getItems()) {
063                if (item instanceof RemoteFieldsHolder) {
064                    List<InputField> translatedFields = ((RemoteFieldsHolder) item)
065                            .fetchAndTranslateRemoteFields(container);
066                    processedItems.addAll(translatedFields);
067                } else {
068                    processedItems.add(item);
069                }
070            }
071    
072            // updated container items
073            container.setItems(processedItems);
074    
075            
076            // invoke hook point for adding components through code
077            ViewLifecycle.getHelper().addCustomContainerComponents((ViewModel)ViewLifecycle.getModel(),
078                    (Container) getElementState().getElement());
079        }
080    
081    }