<commit_before>/*
 * Copyright 2008 FatWire Corporation. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fatwire.gst.foundation.facade.runtag.asset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import COM.FutureTense.Interfaces.FTValList;
import COM.FutureTense.Interfaces.ICS;
import COM.FutureTense.Interfaces.IList;
import COM.FutureTense.Util.IterableIListWrapper;

import com.fatwire.assetapi.data.AssetId;
import com.fatwire.gst.foundation.CSRuntimeException;
import com.openmarket.xcelerate.asset.AssetIdImpl;

import org.apache.commons.logging.Log;

import static com.fatwire.gst.foundation.IListUtils.getStringValue;

/**
 * Utilities for working efficiently with the AssetRelationTree.
 * 
 * @author <NAME>
 * @since Jun 7, 2009
 */
public final class AssetRelationTreeUtils {
    /**
     * Get all of the parent assets in the AssetRelationTree for the specified
     * asset. Association name is required, but expectedParentType is an
     * optional filter argument.
     * 
     * @param ics ICS context
     * @param log logger. May be null.
     * @param child child asset id
     * @param expectedParentType asset type of the parent to be returned. If
     *            null, type of parent is irrelevant.
     * @param associationName name of association to use while looking for
     *            parents. may not be null
     * @return list of parents, never null.
     */
    public static List<AssetId> getAssetRelationTreeParents(ICS ics, Log log, AssetId child, String expectedParentType,
            String associationName) {
        FTValList vl = new FTValList();
        vl.setValString("ftcmd", "findnode");
        vl.setValString("treename", "AssetRelationTree");
        vl.setValString("where", "oid");
        vl.setValString("oid", Long.toString(child.getId()));
        if (ics.TreeManager(vl)) {
            int errno = ics.GetErrno();
            if (errno < 0) {
                switch (errno) {
                    case -111:
                        log.trace("Node not found in AssetRelationTree for asset " + child);
                        return Collections.emptyList();
                    default: {
                        throw new CSRuntimeException("Failed to look up asset " + child + " in AssetRelationTree.",
                                errno);
                    }
                }
            }

            // found node. get its parent
            IList art = ics.GetList("AssetRelationTree");
            ics.RegisterList("AssetRelationTree", null);

            List<AssetId> parents = new ArrayList<AssetId>();

            if (art == null || !art.hasData() || art.numRows() == 0) {
                if (log != null && log.isTraceEnabled()) {
                    log.trace("Failed to locate " + child + " in AssetRelationTree.");<commit_msg>Fix NPE noticed by <NAME> <EMAIL> via yahoogroups.com to ips-link<commit_after>/*
 * Copyright 2008 FatWire Corporation. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fatwire.gst.foundation.facade.runtag.asset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import COM.FutureTense.Interfaces.FTValList;
import COM.FutureTense.Interfaces.ICS;
import COM.FutureTense.Interfaces.IList;
import COM.FutureTense.Util.IterableIListWrapper;

import com.fatwire.assetapi.data.AssetId;
import com.fatwire.gst.foundation.CSRuntimeException;
import com.openmarket.xcelerate.asset.AssetIdImpl;

import org.apache.commons.logging.Log;

import static com.fatwire.gst.foundation.IListUtils.getStringValue;

/**
 * Utilities for working efficiently with the AssetRelationTree.
 * 
 * @author <NAME>
 * @since Jun 7, 2009
 */
public final class AssetRelationTreeUtils {
    /**
     * Get all of the parent assets in the AssetRelationTree for the specified
     * asset. Association name is required, but expectedParentType is an
     * optional filter argument.
     * 
     * @param ics ICS context
     * @param log logger. May be null.
     * @param child child asset id
     * @param expectedParentType asset type of the parent to be returned. If
     *            null, type of parent is irrelevant.
     * @param associationName name of association to use while looking for
     *            parents. may not be null
     * @return list of parents, never null.
     */
    public static List<AssetId> getAssetRelationTreeParents(ICS ics, Log log, AssetId child, String expectedParentType,
            String associationName) {
        FTValList vl = new FTValList();
        vl.setValString("ftcmd", "findnode");
        vl.setValString("treename", "AssetRelationTree");
        vl.setValString("where", "oid");
        vl.setValString("oid", Long.toString(child.getId()));
        if (ics.TreeManager(vl)) {
            int errno = ics.GetErrno();
            if (errno < 0) {
                switch (errno) {
                    case -111:
                        if (log != null && log.isTraceEnabled()) {
                            log.trace("Node not found in AssetRelationTree for asset " + child);
                        }
                        return Collections.emptyList();
                    default: {
                        throw new CSRuntimeException("Failed to look up asset " + child + " in AssetRelationTree.",
                                errno);
                    }
                }
            }

            // found node. get its parent
            IList art = ics.GetList("AssetRelationTree");
            ics.RegisterList("AssetRelationTree", null);

            List<AssetId> parents = new ArrayList<AssetId>();

            if (art == null || !art.hasData() || art.numRows() == 0) {
                if (log != null && log.isTraceEnabled()) {
                    log.trace("Failed to locate " + child + " in AssetRelationTree.");
{'fraction_non_alphanumeric': 0.09418722053944902, 'fraction_numerical': 0.005192557334487235, 'mean_word_length': 2.4275827978250124, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6546540', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1819, 'n_words': 604}