public static IList listSingleAsset(ICS ics, String c, String cid) { ics.RegisterList("spu-gtfa", null); AssetList al = new AssetList(); al.setField("id", cid); al.setType(c); al.setExcludeVoided(true); al.setList("spu-gtfa"); al.execute(ics); IList spugtfa = ics.GetList("spu-gtfa"); ics.RegisterList("spu-gtfa", null); if (spugtfa != null && spugtfa.hasData()) { if (spugtfa.numRows() > 1) { throw new IllegalStateException( "ASSET.LIST failed for " + c + ":" + cid + ": multiple matches found:" + spugtfa.numRows()); } return new IterableIListWrapper(spugtfa).iterator().next(); } else { throw new IllegalStateException( "ASSET.LIST failed for " + c + ":" + cid + ": no data found."); } }
/** * Get a single field from a specified asset. Only one result must be returned from this search or * an exception will be thrown. Pubid is optional. * * @param ics Content Server context object * @param c current asset * @param cid content id * @param field field to get * @return single field value */ public static String getRequiredSingleField(ICS ics, String c, String cid, String field) { ics.RegisterList("spu-gtfa", null); AssetList al = new AssetList(); al.setField("id", cid); al.setType(c); al.setExcludeVoided(true); al.setList("spu-gtfa"); al.execute(ics); IList spugtfa = ics.GetList("spu-gtfa"); ics.RegisterList("spu-gtfa", null); if (spugtfa != null && spugtfa.hasData()) { if (spugtfa.numRows() > 1) { throw new IllegalStateException( "ASSET.LIST failed for " + c + ":" + cid + ": multiple matches found:" + spugtfa.numRows()); } spugtfa.moveTo(1); String ret = IListUtils.getStringValue(spugtfa, field); if (ret == null || ret.length() == 0) { throw new IllegalStateException("No " + field + " found for asset " + c + ":" + cid); } return ret; } else { throw new IllegalStateException( "ASSET.LIST failed for " + c + ":" + cid + ": no data found."); } }
/** * Look up asset id by name * * @param ics context * @param c type * @param name name field value * @return asset id or null if not found. */ public static AssetId lookupAssetId(ICS ics, String c, String name) { if (c == null || c.length() == 0) { throw new IllegalArgumentException("No such asset type: " + c); } if (!SqlHelper.tableExists(ics, c)) { throw new IllegalArgumentException("No such asset type: " + c); } if (name == null || name.length() == 0) { throw new IllegalArgumentException( "Cannot look up asset by name with an actual name. Type: " + c); } ics.RegisterList("spu-gtfa", null); AssetList al = new AssetList(); al.setField("name", name); al.setType(c); al.setExcludeVoided(false); al.setList("spu-gtfa"); al.execute(ics); IList spugtfa = ics.GetList("spu-gtfa"); ics.RegisterList("spu-gtfa", null); if (spugtfa != null && spugtfa.hasData()) { for (Row listRow : new IListIterable(spugtfa)) { return AssetIdUtils.createAssetId(c, listRow.getString("id")); } } return null; }
/** * Reads an IList with collumns <tt>assettype</tt> and <tt>assetid</tt> and creates a Collection * of AssetId objects. * * @param result * @return Collection of AssetIds, never null. */ public static Collection<AssetId> toAssetIdCollection(IList result) { if (result == null || !result.hasData()) return Collections.emptyList(); final List<AssetId> list = new LinkedList<AssetId>(); for (IList row : new IterableIListWrapper(result)) { AssetId id; try { id = AssetIdUtils.createAssetId(row.getValue("assettype"), row.getValue("assetid")); list.add(id); } catch (NoSuchFieldException e) { throw new RuntimeException(e.getMessage()); } } return list; }
/** * Return true if the asset exists in the database, and false if it does not. * * @param ics context * @param c asset type * @param cid asset id * @return true if the asset exists, false if it does not */ public static boolean assetExists(ICS ics, String c, String cid) { if (c == null || c.length() == 0) { return false; } if (cid == null || cid.length() == 0) { return false; } ics.RegisterList("spu-gtfa", null); AssetList al = new AssetList(); al.setField("id", cid); al.setType(c); al.setExcludeVoided(false); al.setList("spu-gtfa"); al.execute(ics); IList spugtfa = ics.GetList("spu-gtfa"); ics.RegisterList("spu-gtfa", null); return spugtfa != null && spugtfa.hasData(); }
public static boolean assetExistsByName(ICS ics, String c, String name) { if (c == null || c.length() == 0) { return false; } if (!SqlHelper.tableExists(ics, c)) { return false; } if (name == null || name.length() == 0) { return false; } ics.RegisterList("spu-gtfa", null); AssetList al = new AssetList(); al.setField("name", name); al.setType(c); al.setExcludeVoided(false); al.setList("spu-gtfa"); al.execute(ics); IList spugtfa = ics.GetList("spu-gtfa"); ics.RegisterList("spu-gtfa", null); return spugtfa != null && spugtfa.hasData(); }