001/**
002 * Copyright 2005-2015 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.finalize;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.uif.UifConstants;
020import org.kuali.rice.krad.uif.component.Component;
021import org.kuali.rice.krad.uif.lifecycle.LifecycleElementState;
022import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
023import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
024import org.kuali.rice.krad.web.form.UifFormBase;
025
026/**
027 * Add the focusId, jumpToId and jumpToName as dataAttributes to the component during the finalize phase.
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class AddFocusAndJumpDataAttributesTask extends ViewLifecycleTaskBase<Component> {
032
033    /**
034     * Constructor.
035     *
036     * @param phase The finalize phase for the component.
037     */
038    public AddFocusAndJumpDataAttributesTask() {
039        super(Component.class);
040    }
041
042    /**
043     * {@inheritDoc}
044     */
045    @Override
046    protected void performLifecycleTask() {
047        LifecycleElementState elementState = getElementState();
048        String phase = elementState.getViewPhase();
049        String viewPath = elementState.getViewPath();
050        if (!ViewLifecycle.isRefreshComponent(phase, viewPath)) {
051            return;
052        }
053
054        Component component = (Component) getElementState().getElement();
055        Object model = ViewLifecycle.getModel();
056
057        UifFormBase formBase = (UifFormBase) model;
058
059        // If AutoFocus then set the focus_id to FIRST field, unless focus_id is also specified
060        if (((UifFormBase) model).getView().getCurrentPage().isAutoFocus() && StringUtils.isNotBlank(formBase.getFocusId())) {
061            component.addDataAttribute(UifConstants.ActionDataAttributes.FOCUS_ID, formBase.getFocusId());
062        } else if (((UifFormBase) model).getView().getCurrentPage().isAutoFocus()) {
063            component.addDataAttribute(UifConstants.ActionDataAttributes.FOCUS_ID, UifConstants.Order.FIRST.name());
064        }
065
066        // Add jumpToId as a data attribute
067        if (StringUtils.isNotBlank(formBase.getJumpToId())) {
068            component.addDataAttribute(UifConstants.ActionDataAttributes.JUMP_TO_ID, formBase.getJumpToId());
069        }
070
071        // Add jumpToName as a data attribute
072        if (StringUtils.isNotBlank(formBase.getJumpToName())) {
073            component.addDataAttribute(UifConstants.ActionDataAttributes.JUMP_TO_NAME, formBase.getJumpToName());
074        }
075    }
076
077}