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.assetapi;
18  
19  import COM.FutureTense.Interfaces.ICS;
20  
21  import com.fatwire.assetapi.data.AssetId;
22  import com.openmarket.xcelerate.asset.AssetIdImpl;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  /**
27   * Utility class for working with Asset IDs
28   * 
29   * @author Tony Field
30   * @since Mar 9, 2011
31   */
32  public final class AssetIdUtils {
33      private AssetIdUtils() {
34      }
35  
36      /**
37       * Convert a string in the form assettype:assetid into an AssetId object.
38       * 
39       * @param s string in the form assettype:assetid
40       * @return AsseId object
41       * @throws IllegalArgumentException if input is bad
42       * 
43       */
44      public static AssetId fromString(String s) {
45          if (s == null)
46              throw new IllegalArgumentException("Invalid null input.");
47          int colon = s.indexOf(":");
48          if (colon < 1)
49              throw new IllegalArgumentException("Invalid input: " + s);
50          if (colon == s.length())
51              throw new IllegalArgumentException("Invalid input: " + s);
52          String type = s.substring(0, colon);
53          String sId = s.substring(colon + 1);
54          try {
55              return new AssetIdImpl(type, Long.valueOf(sId));
56          } catch (NumberFormatException e) {
57              throw new IllegalArgumentException("Invalid input: " + s, e);
58          }
59      }
60  
61      /**
62       * Converts an AssetId into a String of the form assettype:id.
63       * 
64       * @param id
65       * @return the assetid as a String in the form of assettype:id.
66       */
67      public static String toString(AssetId id) {
68          if (id == null)
69              throw new IllegalArgumentException("Invalid null input.");
70          return id.getType() + ":" + Long.toString(id.getId());
71      }
72  
73      /**
74       * Creates an AssetId out of 'c' and 'cid' variables on ICS context.
75       * 
76       * @param ics
77       * @return AssetId from c and cid
78       * @throws IllegalArgumentException if c or cid is blank.
79       */
80      public static AssetId currentId(ICS ics) {
81          String c = ics.GetVar("c");
82          String cid = ics.GetVar("cid");
83          if (StringUtils.isBlank(c)) {
84              throw new IllegalArgumentException(
85                      "CS variable 'c' is not found, cannot make an AssetId of current ICS context");
86          }
87          if (StringUtils.isBlank(cid)) {
88              throw new IllegalArgumentException(
89                      "CS variable 'cid' is not found, cannot make an AssetId of current ICS context");
90          }
91          return new AssetIdImpl(c, Long.parseLong(cid));
92      }
93  
94      /**
95       * Creates an AssetId out of 'p' variable on ICS context with type 'Page'.
96       * 
97       * @param ics
98       * @return AssetId with id from 'p'.
99       * @throws IllegalArgumentException if p is blank.
100      */
101     public static AssetId currentPageId(ICS ics) {
102         String p = ics.GetVar("p");
103 
104         if (StringUtils.isBlank(p)) {
105             throw new IllegalArgumentException(
106                     "CS variable 'p' is not found, cannot make an AssetId of current ICS context");
107         }
108         return new AssetIdImpl("Page", Long.parseLong(p));
109     }
110 
111     /**
112      * Creates an AssetId from c and cid parameters.
113      * 
114      * @param c the assetype
115      * @param cid the assetid, must be a String representing a long value
116      * @return AssetId with c and cid paramters
117      * @throws IllegalArgumentException if c or cid is blank.
118      */
119     public static AssetId createAssetId(String c, String cid) {
120         if (StringUtils.isBlank(c)) {
121             throw new IllegalArgumentException("'c' is blank, cannot make an AssetId of current ICS context");
122         }
123         if (StringUtils.isBlank(cid)) {
124             throw new IllegalArgumentException("'cid' is blank, cannot make an AssetId of current ICS context");
125         }
126         return new AssetIdImpl(c, Long.parseLong(cid));
127     }
128 
129     /**
130      * Creates an AssetId from c and cid parameters.
131      * 
132      * @param c the assetype.
133      * @param cid the assetid.
134      * @return AssetId with c and cid paramters.
135      * @throws IllegalArgumentException is c is blank
136      */
137     public static AssetId createAssetId(String c, long cid) {
138         if (StringUtils.isBlank(c)) {
139             throw new IllegalArgumentException("'c' is blank, cannot make an AssetId of current ICS context");
140         }
141         return new AssetIdImpl(c, cid);
142     }
143 
144 }