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.runtag.asset;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  import COM.FutureTense.Interfaces.IList;
24  import COM.FutureTense.Util.IterableIListWrapper;
25  
26  import com.fatwire.assetapi.data.AssetId;
27  import com.fatwire.gst.foundation.IListUtils;
28  import com.fatwire.gst.foundation.facade.runtag.AbstractTagRunner;
29  import com.openmarket.xcelerate.asset.AssetIdImpl;
30  
31  /**
32   * AssetChildren
33   * <p/>
34   * <ASSET.CHILDREN NAME="assetName" [TYPE="parent assettype"]
35   * [ASSETID="parent assetid"] LIST="listName" [CODE="NameOfAssociation"]
36   * [OBJECTTYPE="typeOfObject"] [OBJECTID="objectID"] [ORDER="nrank"]/>
37   * 
38   * @author Tony Field
39   * @since Sep 28, 2008
40   */
41  public class Children extends AbstractTagRunner {
42      public Children() {
43          super("ASSET.CHILDREN");
44      }
45  
46      public void setName(String name) {
47          set("NAME", name);
48      }
49  
50      public void setType(String type) {
51          set("TYPE", type);
52      }
53  
54      public void setAssetId(String parentId) {
55          set("ASSETID", parentId);
56      }
57  
58      public void setList(String list) {
59          set("LIST", list);
60      }
61  
62      public void setCode(String code) {
63          set("CODE", code);
64      }
65  
66      public void setObjectType(String otype) {
67          set("OBJECTTYPE", otype);
68      }
69  
70      public void setObjectId(String oid) {
71          set("OBJECTID", oid);
72      }
73  
74      public void setOrder(String order) {
75          set("ORDER", order);
76      }
77  
78      @Override
79      protected void handleError(ICS ics) {
80          if (ics.GetErrno() == -111)
81              return;
82          super.handleError(ics);
83      }
84  
85      /**
86       * Look up the single valued named association for a specified asset. If no
87       * associated asset is found an exception is thrown.
88       * 
89       * @param ics context
90       * @param c asset type
91       * @param cid asset id
92       * @param code association name
93       * @return id of the associated asset
94       */
95      public static AssetId getSingleAssociation(ICS ics, String c, String cid, String code) {
96          ics.RegisterList("spu-kids", null);
97          Children ac = new Children();
98          ac.setType(c);
99          ac.setAssetId(cid);
100         ac.setCode(code);
101         ac.setList("spu-kids");
102         ac.execute(ics);
103         IList spukids = ics.GetList("spu-kids");
104         ics.RegisterList("spu-kids", null);
105         if (spukids != null && spukids.hasData()) {
106             if (spukids.numRows() > 1) {
107                 throw new IllegalStateException("Too many kids found associated to: " + c + ":" + cid
108                         + " for association:" + code + ".  expected 1 but got " + spukids.numRows());
109             }
110             spukids.moveTo(1);
111             return new AssetIdImpl(IListUtils.getStringValue(spukids, "otype"), IListUtils.getLongValue(spukids, "oid"));
112         } else {
113             throw new IllegalStateException("No kids found associated to: " + c + ":" + cid + " for association:"
114                     + code + ".  expected 1 but got none.");
115         }
116     }
117 
118     /**
119      * Look up the single valued named association for a specified asset. If no
120      * associated asset is found null is returned
121      * 
122      * @param ics context
123      * @param c asset type
124      * @param cid asset id
125      * @param code association name
126      * @return id of the associated asset
127      */
128     public static AssetId getOptionalSingleAssociation(ICS ics, String c, String cid, String code) {
129         ics.RegisterList("spu-kids", null);
130         Children ac = new Children();
131         ac.setType(c);
132         ac.setAssetId(cid);
133         ac.setCode(code);
134         ac.setList("spu-kids");
135         ac.execute(ics);
136         IList spukids = ics.GetList("spu-kids");
137         ics.RegisterList("spu-kids", null);
138         if (spukids != null && spukids.hasData()) {
139             if (spukids.numRows() > 1) {
140                 throw new IllegalStateException("Too many kids found associated to: " + c + ":" + cid
141                         + " for association:" + code + ".  expected 0 or 1 but got " + spukids.numRows());
142             }
143             spukids.moveTo(1);
144             return new AssetIdImpl(IListUtils.getStringValue(spukids, "otype"), IListUtils.getLongValue(spukids, "oid"));
145         } else {
146             return null;
147         }
148     }
149 
150     /**
151      * Look up the multi-valued named association for a specified asset. If no
152      * associated asset is found an empty list is returned.
153      * 
154      * @param ics context
155      * @param c asset type
156      * @param cid asset id
157      * @param code association name
158      * @return ids of the associated asset
159      */
160     public static List<AssetId> getOptionalMultivaluedAssociation(ICS ics, String c, String cid, String code) {
161         ics.RegisterList("spu-kids", null);
162         Children ac = new Children();
163         ac.setType(c);
164         ac.setAssetId(cid);
165         ac.setCode(code);
166         ac.setList("spu-kids");
167         ac.execute(ics);
168         IList spukids = ics.GetList("spu-kids");
169         ics.RegisterList("spu-kids", null);
170         List<AssetId> result = new ArrayList<AssetId>();
171         if (spukids != null && spukids.hasData()) {
172             for (IList row : new IterableIListWrapper(spukids)) {
173                 result.add(new AssetIdImpl(IListUtils.getStringValue(row, "otype"), IListUtils.getLongValue(row, "oid")));
174 
175             }
176         }
177         return result;
178     }
179 
180     /**
181      * Look up the multi-valued named association for a specified asset. If no
182      * associated asset is found an exception is thrown.
183      * 
184      * @param ics context
185      * @param c asset type
186      * @param cid asset id
187      * @param code association name
188      * @return ids of the associated asset
189      */
190     public static List<AssetId> getMultivaluedAssociation(ICS ics, String c, String cid, String code) {
191         List<AssetId> result = getOptionalMultivaluedAssociation(ics, c, cid, code);
192         if (result.size() == 0) {
193             throw new IllegalStateException("No kids found associated to: " + c + ":" + cid + " for association:"
194                     + code + ".  expected at least one but got none.");
195         }
196         return result;
197     }
198 }