View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.lifecycle.model;
17  
18  import java.lang.annotation.Annotation;
19  import java.util.Map;
20  import java.util.Map.Entry;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.krad.uif.component.ClientSideState;
24  import org.kuali.rice.krad.uif.component.Component;
25  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
26  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
27  import org.kuali.rice.krad.uif.util.CopyUtils;
28  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
29  import org.kuali.rice.krad.uif.view.View;
30  import org.kuali.rice.krad.uif.view.ViewModel;
31  
32  /**
33   * Synchronize client side state for the component.
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class SyncClientSideStateTask extends ViewLifecycleTaskBase<Component> {
38  
39      /**
40       * Default constructor.
41       */
42      public SyncClientSideStateTask() {
43          super(Component.class);
44      }
45  
46      /**
47       * Updates the properties of the given component instance with the value found from the
48       * corresponding map of client state (if found)
49       * 
50       * {@inheritDoc}
51       */
52      @Override
53      protected void performLifecycleTask() {
54          Component component = (Component) getElementState().getElement();
55          ViewModel model = (ViewModel) ViewLifecycle.getModel();
56  
57          // find the map of state that was sent for component (if any)
58          Map<String, Object> clientSideState = model.getClientStateForSyncing();
59          if (!(component instanceof View) && clientSideState.containsKey(component.getId())) {
60              @SuppressWarnings("unchecked")
61              Map<String, Object> componentState =
62                      (Map<String, Object>) clientSideState.get(component.getId());
63              clientSideState = componentState;
64          }
65  
66          // if state was sent, match with fields on the component that are annotated to have client state
67          if ((clientSideState != null) && (!clientSideState.isEmpty())) {
68              Map<String, Annotation> annotatedFields = CopyUtils.getFieldsWithAnnotation(component.getClass(),
69                      ClientSideState.class);
70  
71              for (Entry<String, Annotation> annotatedField : annotatedFields.entrySet()) {
72                  ClientSideState clientSideStateAnnot = (ClientSideState) annotatedField.getValue();
73  
74                  String variableName = clientSideStateAnnot.variableName();
75                  if (StringUtils.isBlank(variableName)) {
76                      variableName = annotatedField.getKey();
77                  }
78  
79                  if (clientSideState.containsKey(variableName)) {
80                      Object value = clientSideState.get(variableName);
81                      ObjectPropertyUtils.setPropertyValue(component, annotatedField.getKey(), value);
82                  }
83              }
84          }
85      }
86  
87  }