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 tools.gsf.facade.mda;
18  
19  import COM.FutureTense.Interfaces.ICS;
20  import com.fatwire.assetapi.data.AssetId;
21  import com.fatwire.mda.Dimension;
22  import com.fatwire.mda.DimensionException;
23  import com.fatwire.mda.DimensionFilterInstance;
24  import com.fatwire.mda.DimensionManager;
25  import com.fatwire.mda.DimensionSetInstance;
26  import com.fatwire.mda.DimensionableAssetManager;
27  import com.fatwire.system.Session;
28  import com.fatwire.system.SessionFactory;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import java.util.Collection;
33  import java.util.List;
34  
35  
36  /**
37   * Miscellaneous utilities for working with dimensions
38   *
39   * @author Tony Field
40   * @since Jun 8, 2009
41   */
42  public final class DimensionUtils {
43      private static final Logger _log = LoggerFactory.getLogger("tools.gsf.facade.mda.DefaultLocaleService");
44  
45      /**
46       * Shorthand function for returning the DimensionableAssetManager given an
47       * ICS context.
48       *
49       * @param ics context
50       * @return dimensionable asset manager
51       */
52      public static DimensionableAssetManager getDAM(ICS ics) {
53          Session session = SessionFactory.getSession(ics);
54          return (DimensionableAssetManager) session.getManager(DimensionableAssetManager.class.getName());
55      }
56  
57      /**
58       * Shorthand function for returning the DimensionManager given an ICS
59       * context
60       *
61       * @param ics context
62       * @return Dimension Manager
63       */
64      public static DimensionManager getDM(ICS ics) {
65          Session session = SessionFactory.getSession(ics);
66          return (DimensionManager) session.getManager(DimensionManager.class.getName());
67      }
68  
69      /**
70       * Return the dimension of the input asset that corresponds to its locale.
71       * If the asset does not have a locale set, returns null
72       *
73       * @param ics context
74       * @param id  asset
75       * @return locale dimension or null
76       */
77      public static Dimension getLocaleAsDimension(ICS ics, AssetId id) {
78          return getLocaleAsDimension(getDAM(ics), id);
79  
80      }
81  
82      /**
83       * Return the dimension of the input asset that corresponds to its locale.
84       * If the asset does not have a locale set, returns null
85       *
86       * @param id  asset
87       * @param dam dimensionable asset manager object
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         }
166         DimensionFilterInstance filter = dimSet.getFilter();
167         if (_log.isTraceEnabled()) {
168             _log.trace("Loading filter. Success? " + (filter != null));
169         }
170         if (filter != null) {
171             filter.setDimensonPreference(preferredDimensions);
172         }
173         return filter;
174     }
175 
176     /**
177      * Main dimension filtering method. Accesses the filter in the dimension
178      * set, configures it with the preferred dimension IDs, then filters the
179      * input assets.
180      *
181      * @param dimensionManager      manager class for Dimension lookups
182      * @param toFilterList          list of input assets that need to be filtered. Often
183      *                              it's just one, but a list is perfectly valid.
184      * @param preferredDimensionIds preferred dimensions to be investigated for
185      *                              a result. Priority preference depends on the configured filter
186      * @param dimSet                DimensionSet to use for filtering.
187      * @return list of assets based on the filtering rules in the dimension
188      * filter from the specified dimension set.
189      * @throws DimensionException in case something goes terribly wrong.
190      */
191     public static Collection<AssetId> filterAssets(DimensionManager dimensionManager, List<AssetId> toFilterList,
192                                                    Collection<AssetId> preferredDimensionIds, DimensionSetInstance dimSet) throws DimensionException {
193         Collection<AssetId> result = getDimensionFilter(dimensionManager, preferredDimensionIds, dimSet).filterAssets(
194                 toFilterList);
195         if (_log.isDebugEnabled()) {
196             _log.debug("Filtered " + toFilterList + " using " + dimSet + ", looking for " + preferredDimensionIds
197                     + " and got " + result);
198         }
199         return result;
200     }
201 
202 }