View Javadoc

1   /*
2    * AbstractDataProviderLinkBuilder.java
3    *
4    * Created on 14 Май 2006 г., 22:18
5    */
6   
7   package org.weda.data.impl;
8   
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  import org.weda.property.ObjectDescriptorRegistry;
13  import org.weda.property.PropertyDescriptor;
14  import org.weda.property.PropertyValue;
15  import org.weda.converter.ValueTypeConverter;
16  import org.weda.data.DataProviderLinkBuilder;
17  import org.weda.data.DataProviderLinkBuilderException;
18  import org.weda.data.DataProviderRegistry;
19  import org.weda.property.impl.ObjectDescriptor;
20  
21  /**
22   *
23   * @author Mikhail Titov
24   */
25  public abstract class AbstractDataProviderLinkBuilder 
26          implements DataProviderLinkBuilder
27  {
28      public final static String PARAM_PREFIX="DID_";
29      public final static String DATA_ID_CLASS_PARAM = "DIDCLASS";
30      
31      private ObjectDescriptorRegistry objectDescriptorRegistry;
32      private ValueTypeConverter converter;
33      private DataProviderRegistry dataProviderRegistry;
34      private PropertyValue propertyValue;
35      
36      public Object getDataIdentificator(Map<String, String> requestParams) 
37          throws DataProviderLinkBuilderException 
38      {
39          try{
40              String dataIdClassName = requestParams.get(DATA_ID_CLASS_PARAM);
41              try{
42                  Class dataIdClass = Class.forName(dataIdClassName);
43                  dataProviderRegistry.checkDataIdentificator(dataIdClass);
44                  Object dataId = dataIdClass.newInstance();
45                  for (Map.Entry<String, String> entry: requestParams.entrySet()){
46                      if (entry.getKey().startsWith(PARAM_PREFIX)){
47                          String propName=
48                                  entry.getKey().substring(PARAM_PREFIX.length());
49                          String strValue=entry.getValue();
50                          PropertyDescriptor propDesc = 
51                                  objectDescriptorRegistry.getPropertyDescriptor(
52                                      dataIdClass, propName);
53                          Integer id = 
54                                  propertyValue.compileSetter(
55                                      dataIdClass, propName);
56                          Object value = converter.convert(
57                                  propDesc.getPropertyClass()
58                                  , strValue
59                                  , propDesc.getPattern());
60                          propertyValue.setValue(dataId, id, value);
61                      }                    
62                  }
63                  return dataId;
64              }catch(ClassNotFoundException e){
65                  throw new DataProviderLinkBuilderException(
66                      String.format(
67                          "Unknown data identificator class (%s)"
68                          , dataIdClassName)
69                      , e);
70              }
71          }catch(Exception e){
72              throw new DataProviderLinkBuilderException(
73                      "Can't extract data identificator from request parameters"
74                      , e);
75          }
76      }
77  
78      public String getLinkToDataViewer(Object dataIdentificator) 
79          throws DataProviderLinkBuilderException 
80      {
81          try{
82              dataProviderRegistry.checkDataIdentificator(
83                      dataIdentificator.getClass());
84              ObjectDescriptor desc = 
85                      objectDescriptorRegistry.get(dataIdentificator.getClass());
86              List<String> props = desc.getReadWriteProperties();
87              Map<String, String> linkParameters = new HashMap<String, String>();
88              linkParameters.put(
89                      DATA_ID_CLASS_PARAM
90                      , dataIdentificator.getClass().getName());
91              for (String propName: props){
92                  Integer id = 
93                          propertyValue.compileGetter(
94                              dataIdentificator.getClass(), propName);
95                  Object value = propertyValue.getValue(dataIdentificator, id);
96                  PropertyDescriptor propDesc = 
97                          desc.getPropertyDescriptor(propName);
98                  String strValue = 
99                      (String)converter.convert(
100                         String.class, value, propDesc.getPattern());
101                 linkParameters.put(PARAM_PREFIX+propName, strValue);
102             }
103             linkParameters.put("TMS", ""+System.currentTimeMillis());
104             return formLink(linkParameters);
105         }catch(Exception e){
106             throw new DataProviderLinkBuilderException(
107                 String.format(
108                     "Can't build link to data viewer for data identificator" +
109                     "(%s)", dataIdentificator)
110                 , e);
111         }
112     }
113         
114     protected abstract String formLink(Map<String, String> parameters);
115     
116     public void setObjectDescriptorRegistry(
117             ObjectDescriptorRegistry objectDescriptorRegistry) 
118     {
119         this.objectDescriptorRegistry = objectDescriptorRegistry;
120     }
121 
122     public void setConverter(ValueTypeConverter converter) {
123         this.converter = converter;
124     }
125 
126     public void setDataProviderRegistry(
127             DataProviderRegistry dataProviderRegistry) 
128     {
129         this.dataProviderRegistry = dataProviderRegistry;
130     }
131 
132     public void setPropertyValue(PropertyValue propertyValue) {
133         this.propertyValue = propertyValue;
134     }        
135 }