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; 017 018import org.kuali.rice.krad.uif.util.ProcessLogger; 019import org.slf4j.Logger; 020import org.slf4j.LoggerFactory; 021 022/** 023 * Base abstract implementation for a lifecycle task. 024 * 025 * @param <T> Top level element type for this task 026 * @author Kuali Rice Team (rice.collab@kuali.org) 027 */ 028public abstract class ViewLifecycleTaskBase<T> implements ViewLifecycleTask<T> { 029 private final Logger LOG = LoggerFactory.getLogger(ViewLifecycleTaskBase.class); 030 031 private final Class<T> elementType; 032 033 private LifecycleElementState elementState; 034 035 /** 036 * Creates a lifecycle processing task for a specific phase. 037 * 038 * @param elementType Top level element type 039 */ 040 protected ViewLifecycleTaskBase(Class<T> elementType) { 041 this.elementType = elementType; 042 } 043 044 /** 045 * Executes the lifecycle task. 046 * 047 * <p> 048 * This method performs state validation and updates component view status. Override 049 * {@link #performLifecycleTask()} to provide task-specific behavior. 050 * </p> 051 * 052 * {@inheritDoc} 053 */ 054 @Override 055 public final void run() { 056 try { 057 if (!getElementType().isInstance(elementState.getElement())) { 058 return; 059 } 060 061 if (ProcessLogger.isTraceActive()) { 062 ProcessLogger.countBegin("lc-task-" + elementState.getViewPhase()); 063 } 064 065 try { 066 performLifecycleTask(); 067 } finally { 068 069 if (ProcessLogger.isTraceActive()) { 070 ProcessLogger.countEnd("lc-task-" + elementState.getViewPhase(), 071 getClass().getName() + " " + elementState.getClass().getName() + " " + elementState 072 .getElement().getClass().getName() + " " + elementState.getElement().getId()); 073 } 074 } 075 076 } catch (Throwable t) { 077 LOG.warn("Error in lifecycle phase " + this, t); 078 079 if (t instanceof RuntimeException) { 080 throw (RuntimeException) t; 081 } else if (t instanceof Error) { 082 throw (Error) t; 083 } else { 084 throw new IllegalStateException("Unexpected error in lifecycle phase " + this, t); 085 } 086 } 087 } 088 089 /** 090 * Performs phase-specific lifecycle processing tasks. 091 */ 092 protected abstract void performLifecycleTask(); 093 094 /** 095 * {@inheritDoc} 096 */ 097 @Override 098 public LifecycleElementState getElementState() { 099 return elementState; 100 } 101 102 /** 103 * Sets the phase on a recycled task. 104 * 105 * @param elementState The phase to set. 106 * @see #getElementState() 107 */ 108 public void setElementState(LifecycleElementState elementState) { 109 this.elementState = elementState; 110 } 111 112 /** 113 * {@inheritDoc} 114 */ 115 @Override 116 public Class<T> getElementType() { 117 return this.elementType; 118 } 119 120 /** 121 * {@inheritDoc} 122 */ 123 @Override 124 public String toString() { 125 return getClass().getSimpleName() 126 + " " 127 + getElementState().getElement().getClass().getSimpleName() 128 + " " 129 + getElementState().getElement().getId(); 130 } 131 132}