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.model;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.uif.component.Component;
020import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
021import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
022import org.kuali.rice.krad.uif.util.ComponentUtils;
023
024/**
025 * Adjusts the id for elements that are within a component configured with a suffix to apply for
026 * all children (such as components that are within a collection line).
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class SuffixIdFromContainerTask extends ViewLifecycleTaskBase<Component> {
031
032    /**
033     * Constructor.
034     *
035     * @param phase The apply model phase for the component.
036     */
037    public SuffixIdFromContainerTask() {
038        super(Component.class);
039    }
040
041    /**
042     *  Pulls the container id suffix from the parent component and updates the id on the given element,
043     *  then sets the container id suffix on the component so the suffixing will apply to all its children.
044     * 
045     * {@inheritDoc}
046     */
047    @Override
048    protected void performLifecycleTask() {
049        Component component = (Component) getElementState().getElement();
050        ViewLifecyclePhase phase = (ViewLifecyclePhase) getElementState();
051        
052        Component parent = phase.getParent();
053        if (parent == null) {
054            return;
055        }
056
057
058        String containerIdSuffix = phase.getParent().getContainerIdSuffix();
059        if (StringUtils.isBlank(parent.getContainerIdSuffix())) {
060            return;
061        }
062                
063        ComponentUtils.updateIdWithSuffix(component, containerIdSuffix);
064
065        // container suffixes should concatenate if multiple are found within a node
066        if (StringUtils.isNotBlank(component.getContainerIdSuffix())) {
067            containerIdSuffix = component.getContainerIdSuffix() + containerIdSuffix;
068        }
069
070        component.setContainerIdSuffix(containerIdSuffix);
071    }
072
073}