View Javadoc

1   /*
2    * Copyright 2008 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  
17  package com.fatwire.gst.foundation.facade.mda;
18  
19  import java.util.Collection;
20  import java.util.List;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  
24  import com.fatwire.assetapi.data.AssetId;
25  import com.fatwire.mda.Dimension;
26  import com.fatwire.mda.DimensionException;
27  import com.fatwire.mda.DimensionFilterInstance;
28  import com.fatwire.mda.DimensionManager;
29  import com.fatwire.mda.DimensionSetInstance;
30  import com.fatwire.mda.DimensionableAssetManager;
31  import com.fatwire.system.Session;
32  import com.fatwire.system.SessionFactory;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * Miscellaneous utilities for working with dimensions
39   * 
40   * @author Tony Field
41   * @since Jun 8, 2009
42   */
43  public final class DimensionUtils {
44      private static final Log _log = LogFactory.getLog(DefaultLocaleService.class.getPackage().getName());
45  
46      /**
47       * Shorthand function for returning the DimensionableAssetManager given an
48       * ICS context.
49       * 
50       * @param ics context
51       * @return dimensionable asset manager
52       */
53      public static DimensionableAssetManager getDAM(ICS ics) {
54          Session session = SessionFactory.getSession(ics);
55          return (DimensionableAssetManager) session.getManager(DimensionableAssetManager.class.getName());
56      }
57  
58      /**
59       * Shorthand function for returning the DimensionManager given an ICS
60       * context
61       * 
62       * @param ics context
63       * @return Dimension Manager
64       */
65      public static DimensionManager getDM(ICS ics) {
66          Session session = SessionFactory.getSession(ics);
67          return (DimensionManager) session.getManager(DimensionManager.class.getName());
68      }
69  
70      /**
71       * Return the dimension of the input asset that corresponds to its locale.
72       * If the asset does not have a locale set, returns null
73       * 
74       * @param ics context
75       * @param id asset
76       * @return locale dimension or null
77       */
78      public static Dimension getLocaleAsDimension(ICS ics, AssetId id) {
79          return getLocaleAsDimension(getDAM(ics), id);
80  
81      }
82  
83      /**
84       * Return the dimension of the input asset that corresponds to its locale.
85       * If the asset does not have a locale set, returns null
86       * 
87       * @param id asset
88       * @return locale dimension or null
89       */
90      public static Dimension getLocaleAsDimension(DimensionableAssetManager dam, AssetId id) {
91          Collection<Dimension> dims = dam.getDimensionsForAsset(id);
92          for (Dimension dim : dims) {
93              if ("locale".equalsIgnoreCase(dim.getGroup())) {
94                  return dim;
95              }
96          }
97          return null;
98      }
99  
100     /**
101      * Get the id of the dimension asset for the name specified
102      * 
103      * @param ics context
104      * @param name dimension name, or locale
105      * @return dimension id, -1 if not found.
106      */
107     public static long getDimensionIdForName(ICS ics, String name) {
108         AssetId id = getDimensionAssetIdForName(ics, name);
109         return id == null ? -1 : id.getId();
110     }
111 
112     /**
113      * Get the AssetId of the dimension asset for the name specified
114      * 
115      * @param ics context
116      * @param name dimension name, or locale
117      * @return dimension id
118      */
119     public static AssetId getDimensionAssetIdForName(ICS ics, String name) {
120         Dimension dim = getDimensionForName(ics, name);
121         return dim == null ? null : dim.getId();
122     }
123 
124     /**
125      * Get the AssetId of the dimension asset for the name specified
126      * 
127      * @param ics context
128      * @param name dimension name, or locale
129      * @return dimension id
130      */
131     public static Dimension getDimensionForName(ICS ics, String name) {
132         return getDM(ics).loadDimension(name);
133 
134     }
135 
136     /**
137      * Shorthand function to get the name given a dimension ID specified.
138      * 
139      * @param ics context
140      * @param dimensionid ID of a locale. Note the dimension group is not
141      *            verified
142      * @return dimension name, or locale name, like en_CA.
143      */
144     public static String getNameForDimensionId(ICS ics, long dimensionid) {
145         return getDM(ics).loadDimension(dimensionid).getName();
146     }
147 
148     /**
149      * Method to get a fully-populated dimension filter, given the specified
150      * input params. This can be used for filtering.
151      * 
152      * @param dimensionManager manager class for Dimension lookups
153      * @param preferredDimensionIds preferred dimensions to be investigated for
154      *            a result. Priority preference depends on the configured filter
155      * @param dimSet DimensionSet to use for filtering.
156      * @return list of assets based on the filtering rules in the dimension
157      *         filter from the specified dimension set.
158      * @throws DimensionException in case something goes terribly wrong.
159      */
160     public static DimensionFilterInstance getDimensionFilter(DimensionManager dimensionManager,
161             Collection<AssetId> preferredDimensionIds, DimensionSetInstance dimSet) throws DimensionException {
162         List<Dimension> preferredDimensions = dimensionManager.loadDimensions(preferredDimensionIds);
163         if (_log.isTraceEnabled())
164             _log.trace("Loaded preferred dimensions and found " + preferredDimensions.size());
165         DimensionFilterInstance filter = dimSet.getFilter();
166         if (_log.isTraceEnabled())
167             _log.trace("Loading filter. Success? " + (filter != null));
168         if (filter != null)
169             filter.setDimensonPreference(preferredDimensions);
170         return filter;
171     }
172 
173     /**
174      * Main dimension filtering method. Accesses the filter in the dimension
175      * set, configures it with the preferred dimension IDs, then filters the
176      * input assets.
177      * 
178      * @param dimensionManager manager class for Dimension lookups
179      * @param toFilterList list of input assets that need to be filtered. Often
180      *            it's just one, but a list is perfectly valid.
181      * @param preferredDimensionIds preferred dimensions to be investigated for
182      *            a result. Priority preference depends on the configured filter
183      * @param dimSet DimensionSet to use for filtering.
184      * @return list of assets based on the filtering rules in the dimension
185      *         filter from the specified dimension set.
186      * @throws DimensionException in case something goes terribly wrong.
187      */
188     public static Collection<AssetId> filterAssets(DimensionManager dimensionManager, List<AssetId> toFilterList,
189             Collection<AssetId> preferredDimensionIds, DimensionSetInstance dimSet) throws DimensionException {
190         Collection<AssetId> result = getDimensionFilter(dimensionManager, preferredDimensionIds, dimSet).filterAssets(
191                 toFilterList);
192         if (_log.isDebugEnabled())
193             _log.debug("Filtered " + toFilterList + " using " + dimSet + ", looking for " + preferredDimensionIds
194                     + " and got " + result);
195         return result;
196     }
197 
198 }