View Javadoc

1   /*
2    * Copyright 2010 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.properties;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import COM.FutureTense.Interfaces.ICS;
29  import COM.FutureTense.Util.ftErrors;
30  
31  import com.fatwire.assetapi.common.AssetAccessException;
32  import com.fatwire.assetapi.common.SiteAccessException;
33  import com.fatwire.assetapi.data.AssetData;
34  import com.fatwire.assetapi.data.AssetDataManager;
35  import com.fatwire.assetapi.data.AssetId;
36  import com.fatwire.assetapi.data.AttributeData;
37  import com.fatwire.assetapi.data.MutableAssetData;
38  import com.fatwire.assetapi.query.OpTypeEnum;
39  import com.fatwire.assetapi.query.Query;
40  import com.fatwire.assetapi.site.SiteInfo;
41  import com.fatwire.assetapi.site.SiteManager;
42  import com.fatwire.gst.foundation.CSRuntimeException;
43  import com.fatwire.gst.foundation.facade.assetapi.QueryBuilder;
44  import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAsset;
45  import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAssetAccess;
46  import com.fatwire.gst.foundation.facade.runtag.asset.AssetList;
47  import com.fatwire.system.Session;
48  import com.fatwire.system.SessionFactory;
49  
50  /**
51   * Class representing properties stored as an asset. Can be basic or flex but
52   * fields are specifically defined
53   * 
54   * @author Tony Field
55   * @since 11-09-02
56   */
57  // TODO: Figure out how to assign these to a give site/publication
58  public final class AssetApiPropertyDao implements PropertyDao {
59      private static final Log LOG = LogFactory.getLog("com.fatwire.gst.foundation.properties");
60  
61      public static final String TYPE = "GSTProperty";
62      public static final String SUBTYPE = "GSTProperty";
63      private static final Query LOAD_ALL_QRY = new QueryBuilder(TYPE, SUBTYPE)
64              .attributes("name", "description", "value").condition("status", OpTypeEnum.NOT_EQUALS, "VO")
65              .setBasicSearch(true).toQuery();
66  
67      private final Map<String, Property> _props;
68      private final ICS ics;
69      private AssetDataManager assetDataManager = null;
70  
71      public static final PropertyDao getInstance(ICS ics) {
72          if (ics == null) {
73              throw new IllegalArgumentException("ics must not be null.");
74          }
75  
76          Object o = ics.GetObj(PropertyDao.class.getName());
77          if (o instanceof PropertyDao == false) {
78              o = newInstance(ics);
79              ics.SetObj(PropertyDao.class.getName(), o);
80          }
81          return (PropertyDao) o;
82      }
83  
84      public static PropertyDao newInstance(ICS ics) {
85          if (ics == null) {
86              throw new IllegalArgumentException("ics must not be null.");
87          }
88  
89          return new AssetApiPropertyDao(ics);
90      }
91  
92      private AssetApiPropertyDao(ICS ics) {
93          this.ics = ics;
94          this.assetDataManager = (AssetDataManager)SessionFactory.getSession(ics).getManager(AssetDataManager.class.getName());
95          _props = new HashMap<String, Property>();
96          TemplateAssetAccess mgr = new TemplateAssetAccess(ics);
97  
98          LOG.trace("Loading all GSTProperties");
99          for (TemplateAsset d : mgr.query(LOAD_ALL_QRY)) {
100             String name = d.asString("name");
101             PropertyImpl p = new PropertyImpl(name, d.asString("description"), d.asString("value"));
102             _props.put(name, p);
103             if (LOG.isTraceEnabled())
104                 LOG.trace("Loaded property: " + p);
105         }
106     }
107 
108     public Property getProperty(String name) {
109         return _props.get(name);
110     }
111 
112     public Collection<String> getPropertyNames() {
113         return _props.keySet();
114     }
115 
116     /**
117      * Set (or re-set) a property value
118      *
119      * @param property property object with name and value
120      */
121     public void setProperty(Property property) {
122         if (property == null) throw new IllegalArgumentException("Can't set a null property object");
123         if (_props.containsKey(property.getName())) {
124             // replace asset
125             try {
126                 Session ses = SessionFactory.getSession();
127                 AssetId id = AssetList.lookupAssetId(ics, "GSTProperty", property.getName());
128                 ArrayList<AssetData> sAssets = new ArrayList<AssetData>();
129                 for (MutableAssetData data : assetDataManager.readForUpdate(Arrays.asList(id))) {
130                     // sorry can't reset 'name'
131                     data.getAttributeData("description").setData(property.getDescription());
132                     data.getAttributeData("value").setData(property.asString());
133                     appendCurrentToPublist(data.getAttributeData("Publist"));
134                     sAssets.add(data);
135                 }
136                 assetDataManager.update(sAssets);
137             } catch (Exception e) {
138                 throw new CSRuntimeException("Could not update property " + property, ftErrors.exceptionerr, e);
139             }
140         } else {
141             // add asset
142             try {
143                 Session ses = SessionFactory.getSession();
144                 MutableAssetData data = assetDataManager.newAssetData("GSTProperty", "GSTProperty");
145                 data.getAttributeData("name").setData(property.getName());
146                 data.getAttributeData("description").setData(property.getDescription());
147                 data.getAttributeData("value").setData(property.asString());
148                 appendCurrentToPublist(data.getAttributeData("Publist"));
149                 assetDataManager.insert(Arrays.<AssetData>asList(data));
150             } catch (AssetAccessException e) {
151                 throw new CSRuntimeException("Could not add new property " + property, ftErrors.exceptionerr, e);
152             }
153         }
154         _props.put(property.getName(), property); // update cache
155     }
156 
157     private void appendCurrentToPublist(AttributeData data) {
158         String currentPub = getCurrentSite();
159         if (currentPub != null) {
160             HashSet<String> pubs = new HashSet<String>();
161             pubs.add(currentPub);
162             pubs.addAll(data.getDataAsList());
163             data.setDataAsList(new ArrayList<String>(pubs));
164         }
165     }
166 
167     private String getCurrentSite() {
168         String pubid = ics.GetSSVar("pubid");
169         if (pubid != null) {
170             long id = Long.valueOf(pubid);
171             SiteManager sm = (SiteManager) SessionFactory.getSession().getManager(SiteManager.class.getName());
172             try {
173                 for (SiteInfo si : sm.list()) {
174                     if (si.getId() == id) return si.getName();
175                 }
176             } catch (SiteAccessException e) {
177                 throw new CSRuntimeException("Could not determine name of current site: " + e, ftErrors.exceptionerr, e);
178             }
179         }
180         return null;
181     }
182 
183     /**
184      * Convenience method to set (or re-set) a property value
185      *
186      * @param name        property name
187      * @param description property description (optional)
188      * @param value       value as a string
189      */
190     public void setProperty(String name, String description, String value) {
191         if (name == null) throw new IllegalArgumentException("Cannot set a null property name");
192         setProperty(new PropertyImpl(name, description, value));
193     }
194 
195     public void addToSite(String name, String ... site) {
196         if (name == null) throw new IllegalArgumentException("Invalid property name null");
197         AssetId id = AssetList.lookupAssetId(ics, "GSTProperty", name);
198         if (id == null) throw new IllegalArgumentException("Could not locate property "+id);
199 
200         try {
201             ArrayList<AssetData> sAssets = new ArrayList<AssetData>();
202             for (MutableAssetData data : assetDataManager.readForUpdate(Arrays.asList(id))) {
203                 HashSet<String> pubs = new HashSet<String>();
204                 pubs.addAll(Arrays.asList(site));
205                 AttributeData publistData = data.getAttributeData("Publist");
206                 pubs.addAll(publistData.getDataAsList());
207                 publistData.setDataAsList(new ArrayList(pubs));
208                 sAssets.add(data);
209             }
210             assetDataManager.update(sAssets);
211         } catch (AssetAccessException e) {
212             throw new CSRuntimeException("Failure adding property "+name+" to sites "+site, ftErrors.exceptionerr, e);
213         }
214     }
215 }