<commit_before>/*
 * Copyright 2010 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.properties;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import COM.FutureTense.Interfaces.ICS;
import COM.FutureTense.Util.ftErrors;

import com.fatwire.assetapi.common.AssetAccessException;
import com.fatwire.assetapi.data.AssetData;
import com.fatwire.assetapi.data.AssetDataManager;
import com.fatwire.assetapi.query.Condition;
import com.fatwire.assetapi.query.ConditionFactory;
import com.fatwire.assetapi.query.OpTypeEnum;
import com.fatwire.assetapi.query.SimpleQuery;
import com.fatwire.gst.foundation.CSRuntimeException;
import com.fatwire.gst.foundation.facade.assetapi.AttributeDataUtils;
import com.fatwire.system.SessionFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class representing properties stored as an asset.  Can be basic or flex but fields are specifically defined
 *
 * @author <NAME>
 * @since 11-09-02
 */
public final class AssetApiPropertyDao implements PropertyDao {
    private static final Log LOG = LogFactory.getLog("com.fatwire.gst.foundation.properties");

    public static final String TYPE = "GSTProperty";
    public static final String SUBTYPE = "GSTProperty";
    public static final String[] ATTRS = {"name", "description", "value"};
    private static final Condition COND = ConditionFactory.createCondition("status", OpTypeEnum.NOT_EQUALS, "VO"); // not valid for flex?
    //private static final SimpleQuery LOAD_ALL_QRY = new SimpleQuery(TYPE, SUBTYPE, COND, Arrays.asList(ATTRS));
    private static final SimpleQuery LOAD_ALL_QRY = new SimpleQuery(TYPE, SUBTYPE, null, Arrays.asList(ATTRS));

    private final Map<String, Property> _props;


    public static final PropertyDao getInstance(ICS ics) {
        if (ics == null) {
            throw new IllegalArgumentException("ics must not be null.");
        }

        Object o = ics.GetObj(PropertyDao.class.getName());
        if (o == null) {
            o = newInstance(ics);
            ics.SetObj(PropertyDao.class.getName(), o);
        }
        return (PropertyDao) o;
    }

    public static PropertyDao newInstance(ICS ics) {
        if (ics == null) {
            throw new IllegalArgumentException("ics must not be null.");
        }

        return new AssetApiPropertyDao(ics);
    }

    private AssetApiPropertyDao(ICS ics) {
        _props = new HashMap<String, Property>();
        AssetDataManager mgr = (AssetDataManager) SessionFactory.getSession(ics).getManager(AssetDataManager.class.getName());
        try {
            LOG.trace("Loading all GSTProperties");
            for (AssetData d : mgr.read(LOAD_ALL_QRY)) {
                String name = AttributeDataUtils.asString(d.getAttributeData("name"));
                PropertyImpl p = new PropertyImpl(name, AttributeDataUtils.asString(d.getAttributeData("description")), AttributeDataUtils.asString(d.getAttributeData("value")));
                _props.put(name, p);
                if (LOG.isTraceEnabled()) LOG.trace("Loaded property: "+p);
            }
        } catch (AssetAccessException e) {
            throw new CSRuntimeException("Failure loading property asset", ftErrors.exceptionerr, e);
        }
    }

    public Property getProperty(String name) {
        return _props.get(name);
    }

    public Collection<String> getPropertyNames() {
        return _props.keySet();
    }
}<commit_msg>Refactoring  with GSF helper classes for better readability.<commit_after>/*
 * Copyright 2010 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.properties;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import COM.FutureTense.Interfaces.ICS;

import com.fatwire.assetapi.query.OpTypeEnum;
import com.fatwire.assetapi.query.Query;
import com.fatwire.gst.foundation.facade.assetapi.QueryBuilder;
import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAsset;
import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAssetAccess;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class representing properties stored as an asset. Can be basic or flex but
 * fields are specifically defined
 * 
 * @author <NAME>
 * @since 11-09-02
 */
public final class AssetApiPropertyDao implements PropertyDao {
    private static final Log LOG = LogFactory.getLog("com.fatwire.gst.foundation.properties");

    public static final String TYPE = "GSTProperty";
    public static final String SUBTYPE = "GSTProperty";
    private static final Query LOAD_ALL_QRY = new QueryBuilder(TYPE, SUBTYPE)
            .attributes("name", "description", "value").and("status", OpTypeEnum.NOT_EQUALS, "VO").setBasicSearch(true)
            .toQuery();

    private final Map<String, Property> _props;

    public static final PropertyDao getInstance(ICS ics) {
        if (ics == null) {
            throw new IllegalArgumentException("ics must not be null.");
        }

        Object o = ics.GetObj(PropertyDao.class.getName());
        if (o == null) {
            o = newInstance(ics);
            ics.SetObj(PropertyDao.class.getName(), o);
        }
        return (PropertyDao) o;
    }

    public static PropertyDao newInstance(ICS ics) {
        if (ics == null) {
            throw new IllegalArgumentException("ics must not be null.");
        }

        return new AssetApiPropertyDao(ics);
    }

    private AssetApiPropertyDao(ICS ics) {
        _props = new HashMap<String, Property>();
        TemplateAssetAccess mgr = new TemplateAssetAccess(ics);

        LOG.trace("Loading all GSTProperties");
        for (TemplateAsset d : mgr.query(LOAD_ALL_QRY)) {
            String name = d.asString("name");
            PropertyImpl p = new PropertyImpl(name, d.asString("description"), d.asString("value"));
            _props.put(name, p);
            if (LOG.isTraceEnabled())
                LOG.trace("Loaded property: " + p);
        }
    }

    public Property getProperty(String name) {
        return _props.get(name);
    }

    public Collection<String> getPropertyNames() {
        return _props.keySet();
    }
}
{'fraction_non_alphanumeric': 0.10137060659519609, 'fraction_numerical': 0.003799701452028769, 'mean_word_length': 3.3429581614614023, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7154535', 'n_tokens_mistral': 2077, 'n_tokens_neox': 2023, 'n_words': 591}