View Javadoc

1   /*
2    * $Id: BasicGenerationResourceInformation.java,v 1.3 2010/08/10 14:02:30 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.contentAccessor;
5   
6   import java.util.Date;
7   
8   import javax.faces.component.UIComponent;
9   import javax.faces.component.UIComponentBase;
10  import javax.faces.context.FacesContext;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.rcfaces.core.internal.lang.StringAppender;
15  import org.rcfaces.core.internal.renderkit.IComponentRenderContext;
16  import org.rcfaces.core.internal.version.HashCodeTools;
17  import org.rcfaces.core.model.IFilterProperties;
18  
19  /**
20   * 
21   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
22   * @version $Revision: 1.3 $ $Date: 2010/08/10 14:02:30 $
23   */
24  public class BasicGenerationResourceInformation extends AbstractInformation
25          implements IGenerationResourceInformation {
26  
27      private static final Log LOG = LogFactory
28              .getLog(BasicGenerationResourceInformation.class);
29  
30      private static final String COMPONENT_CLIENT_ID_PROPERTY = "org.rcfaces.org.COMPONENT_CLIENT_ID_PROPERTY";
31  
32      private transient UIComponent component;
33  
34      private IFilterProperties filterProperties;
35  
36      private boolean processAtRequest;
37  
38      public BasicGenerationResourceInformation() {
39      }
40  
41      public BasicGenerationResourceInformation(
42              IComponentRenderContext componentRenderContext) {
43          this(componentRenderContext.getComponent(), componentRenderContext
44                  .getComponentClientId());
45      }
46  
47      public BasicGenerationResourceInformation(UIComponent component,
48              String clientId) {
49          setComponent(component, clientId);
50      }
51  
52      public UIComponent getComponent() {
53          return component;
54      }
55  
56      public void setComponent(IComponentRenderContext componentRenderContext) {
57          setComponent(componentRenderContext.getComponent(),
58                  componentRenderContext.getComponentClientId());
59      }
60  
61      public void setComponent(UIComponent component) {
62          setComponent(component, (FacesContext) null);
63      }
64  
65      public void setComponent(UIComponent component, FacesContext facesContext) {
66          if (facesContext == null) {
67              facesContext = FacesContext.getCurrentInstance();
68          }
69  
70          String clientId = null;
71          if (facesContext != null) {
72              clientId = component.getClientId(facesContext);
73          }
74  
75          setComponent(component, clientId);
76      }
77  
78      public void setComponent(UIComponent component, String clientId) {
79          this.component = component;
80          setComponentClientId(clientId);
81      }
82  
83      public String getComponentClientId() {
84          return (String) getAttribute(COMPONENT_CLIENT_ID_PROPERTY);
85      }
86  
87      public void setComponentClientId(String componentClientId) {
88          setAttribute(COMPONENT_CLIENT_ID_PROPERTY, componentClientId);
89      }
90  
91      public final IFilterProperties getFilterProperties() {
92          return filterProperties;
93      }
94  
95      public final void setFilterProperties(IFilterProperties filterProperties) {
96          this.filterProperties = filterProperties;
97      }
98  
99      public void restoreState(FacesContext context, Object state) {
100         Object states[] = (Object[]) state;
101 
102         super.restoreState(context, states[0]);
103 
104         filterProperties = (IFilterProperties) UIComponentBase
105                 .restoreAttachedState(context, states[1]);
106 
107         // Pas la peine de traiter processAtRequest !!
108     }
109 
110     public Object saveState(FacesContext context) {
111         Object states[] = new Object[2];
112 
113         states[0] = super.saveState(context);
114 
115         states[1] = UIComponentBase
116                 .saveAttachedState(context, filterProperties);
117 
118         // Pas la peine de traiter processAtRequest !!
119 
120         return states;
121     }
122 
123     public final boolean isProcessAtRequest() {
124         return processAtRequest;
125     }
126 
127     public final void setProcessAtRequest(boolean processAtRequest) {
128         this.processAtRequest = processAtRequest;
129     }
130 
131     public String getResponseSuffix() {
132         return (String) getAttribute(RESPONSE_URL_SUFFIX_PROPERTY);
133     }
134 
135     public void setResponseSuffix(String suffix) {
136         setAttribute(RESPONSE_URL_SUFFIX_PROPERTY, suffix);
137     }
138 
139     public final String getResponseMimeType() {
140         return (String) getAttribute(RESPONSE_MIME_TYPE_PROPERTY);
141     }
142 
143     public final void setResponseMimeType(String contentType) {
144         setAttribute(RESPONSE_MIME_TYPE_PROPERTY, contentType);
145     }
146 
147     public void setResponseLastModified(long l) {
148         setAttribute(RESPONSE_LAST_MODIFIED_PROPERTY, new Long(l));
149     }
150 
151     public void setResponseLastModified(Date d) {
152         setAttribute(RESPONSE_LAST_MODIFIED_PROPERTY, d);
153     }
154 
155     public long getResponseLastModified() {
156         Object l = getAttribute(RESPONSE_LAST_MODIFIED_PROPERTY);
157 
158         if (l instanceof Long) {
159             return ((Long) l).longValue();
160         }
161 
162         if (l instanceof Date) {
163             return ((Date) l).getTime();
164         }
165 
166         return -1;
167     }
168 
169     public void setComputeResourceKeyFromGenerationInformation(boolean b) {
170         setAttribute(COMPUTE_RESOURCE_KEY_FROM_GENERATION_INFORMATION, Boolean
171                 .valueOf(b));
172     }
173 
174     public boolean getComputeResourceKeyFromGenerationInformation() {
175         return Boolean.TRUE
176                 .equals(getAttribute(COMPUTE_RESOURCE_KEY_FROM_GENERATION_INFORMATION));
177     }
178 
179     public static String generateResourceKeyFromGenerationInformation(
180             IGenerationResourceInformation generationInformation) {
181 
182         StringAppender sa = new StringAppender(16000);
183         ((IResourceKeyParticipant) generationInformation).participeKey(sa);
184 
185         return HashCodeTools.compute(null, "#resourceKey", sa, 0);
186     }
187 
188 }