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