View Javadoc

1   /*
2    * Copyright 2011 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 com.fatwire.gst.foundation.controller.action.support;
17  
18  import COM.FutureTense.Interfaces.ICS;
19  
20  import com.fatwire.gst.foundation.controller.AssetIdWithSite;
21  import com.fatwire.gst.foundation.controller.action.AnnotationBinder;
22  import com.fatwire.gst.foundation.controller.action.AnnotationInjector;
23  import com.fatwire.gst.foundation.controller.action.Factory;
24  import com.fatwire.gst.foundation.controller.action.FactoryProducer;
25  import com.fatwire.gst.foundation.controller.action.Injector;
26  import com.fatwire.gst.foundation.mapping.MappingInjector;
27  
28  public class DefaultAnnotationInjector implements Injector {
29  
30      private FactoryProducer factoryFactory;
31  
32      public DefaultAnnotationInjector() {
33          super();
34  
35      }
36  
37      public DefaultAnnotationInjector(FactoryProducer factoryFactory) {
38          super();
39          this.factoryFactory = factoryFactory;
40      }
41  
42      @Override
43      public void inject(ICS ics, Object action) {
44          final Factory factory = getFactory(ics);
45          AnnotationInjector.inject(action, factory);
46          AnnotationBinder.bind(action, ics);
47          final AssetIdWithSite id = figureOutTemplateOrCSElementId(ics);
48          if (id != null) {
49              MappingInjector.inject(action, factory, id);
50          }
51  
52      }
53  
54      private AssetIdWithSite figureOutTemplateOrCSElementId(final ICS ics) {
55          String eid = ics.GetVar("eid");
56          if (eid != null) {
57              return new AssetIdWithSite("CSElement", Long.parseLong(eid), ics.GetVar("site"));
58          }
59          eid = ics.GetVar("tid");
60          if (eid != null) {
61              return new AssetIdWithSite("Template", Long.parseLong(eid), ics.GetVar("site"));
62          }
63          return null;
64      }
65  
66      protected Factory getFactory(final ICS ics) {
67  
68          final Object o = ics.GetObj(Factory.class.getName());
69          if (o instanceof Factory) {
70              return (Factory) o;
71          }
72          Factory factory = null;
73  
74          factory = getFactoryFactory().getFactory(ics);
75  
76          ics.SetObj(Factory.class.getName(), factory);
77          return factory;
78      }
79  
80      public FactoryProducer getFactoryFactory() {
81          return factoryFactory;
82      }
83  
84  //    public void setFactoryFactory(FactoryProducer factoryFactory) {
85  //        Assert.notNull(factoryFactory);
86  //        this.factoryFactory = factoryFactory;
87  //    }
88  }