<commit_before>/**
 * File:   QuarterbackSvc.java
 * Author: <NAME>
 * Date:   3 oct. 2011
 */
package org.psem2m.demo.data.server.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Unbind;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.framework.BundleException;
import org.psem2m.demo.data.cache.IDataCache;
import org.psem2m.demo.data.server.IQuarterback;
import org.psem2m.demo.erp.api.beans.CCartLine;
import org.psem2m.demo.erp.api.beans.CErpActionReport;
import org.psem2m.demo.erp.api.beans.CachedItemBean;
import org.psem2m.demo.erp.api.beans.CachedItemStockBean;
import org.psem2m.demo.erp.api.beans.IQualityLevels;
import org.psem2m.demo.erp.api.beans.ItemBean;
import org.psem2m.demo.erp.api.services.IErpDataProxy;
import org.psem2m.isolates.base.IIsolateLoggerSvc;
import org.psem2m.isolates.base.activators.CPojoBase;

/**
 * Main strategy handler
 * 
 * @author <NAME>
 */
@Component(name = "demo-quarterback-factory", publicFactory = false)
@Provides(specifications = IQuarterback.class)
@Instantiate(name = "demo-quarterback")
public class QuarterbackSvc extends CPojoBase implements IQuarterback {

    /** ERP Proxy variable iPOJO ID */
    private static final String IPOJO_ID_ERP = "erp-proxy";

    /** ERP Proxy presence flag */
    @ServiceProperty(name = "erp.available")
    private boolean isProxyAvailable = false;

    /** The internal cache */
    @Requires
    private IDataCache pCache;

    /** ERP Proxy (imported service) */
    @Requires(id = IPOJO_ID_ERP, optional = true)
    private IErpDataProxy pErpProxy;

    /** Log service */
    @Requires
    private IIsolateLoggerSvc pLogger;

    /**
     * Default constructor
     */
    public QuarterbackSvc() {

        super();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.psem2m.demo.erp.api.services.IErpData#applyCart(org.psem2m.demo.erp
     * .api.beans.CCartLine[])
     */
    @Override
    public CErpActionReport applyCart(final CCartLine[] aCartLines) {

        StringBuilder wSB = new StringBuilder();

        wSB.append(String.format("Received cart : [%d] lines : [ ",
                aCartLines.length));
        int wI = 0;
        for (CCartLine wCartLine : aCartLines) {
            if (wI > 0) {
                wSB.append(',');
            }
            wSB.append(String.format("{%d=>%s }", wI, wCartLine.toString()));
            wI++;
        }
        wSB.append("]");

        return new CErpActionReport(
                java.net.HttpURLConnection.HTTP_NOT_IMPLEMENTED,
                "applyCart() must be implemented.", wSB.toString());
    }

    /**
     * Called by iPOJO when the ERP proxy is bound
     * 
     * @param aErpDataProxy
     *            The ERP Proxy service
     */
    @Bind(id = IPOJO_ID_ERP)
    protected void bindErp(final IErpDataProxy aErpDataProxy) {

        // Update the flag
        isProxyAvailable = true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.demo.erp.api.services.IErpData#getItem(java.lang.String)
     */
    @Override
    public CachedItemBean getItem(final String aItemId) {

        if (isProxyAvailable) {
            // Try with the ERP

            try {
                final ItemBean itemBean = pErpProxy.getItem(aItemId);

                if (itemBean != null) {
                    // Update the cache
                    pCache.updateItemBean(aItemId, itemBean);

                    // Return the bean
                    return new CachedItemBean(itemBean,
                            IQualityLevels.CACHE_LEVEL_SYNC);
                }

            } catch (Exception e) {
                // An error occurred using the ERP, continue with the cache
            }
        }

        // The ERP failed
        synchronized (pCache) {

            final ItemBean item = pCache.getItem(aItemId);
            final long itemAge = pCache.getItemInformationAge(aItemId);

            if (item != null && itemAge != -1) {<commit_msg>La version cache de getItem utilise getRandomItem<commit_after>/**
 * File:   QuarterbackSvc.java
 * Author: <NAME>
 * Date:   3 oct. 2011
 */
package org.psem2m.demo.data.server.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Unbind;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.framework.BundleException;
import org.psem2m.demo.data.cache.IDataCache;
import org.psem2m.demo.data.server.IQuarterback;
import org.psem2m.demo.erp.api.beans.CCartLine;
import org.psem2m.demo.erp.api.beans.CErpActionReport;
import org.psem2m.demo.erp.api.beans.CachedItemBean;
import org.psem2m.demo.erp.api.beans.CachedItemStockBean;
import org.psem2m.demo.erp.api.beans.IQualityLevels;
import org.psem2m.demo.erp.api.beans.ItemBean;
import org.psem2m.demo.erp.api.services.IErpDataProxy;
import org.psem2m.isolates.base.IIsolateLoggerSvc;
import org.psem2m.isolates.base.activators.CPojoBase;

/**
 * Main strategy handler
 * 
 * @author <NAME>
 */
@Component(name = "demo-quarterback-factory", publicFactory = false)
@Provides(specifications = IQuarterback.class)
@Instantiate(name = "demo-quarterback")
public class QuarterbackSvc extends CPojoBase implements IQuarterback {

    /** ERP Proxy variable iPOJO ID */
    private static final String IPOJO_ID_ERP = "erp-proxy";

    /** ERP Proxy presence flag */
    @ServiceProperty(name = "erp.available")
    private boolean isProxyAvailable = false;

    /** The internal cache */
    @Requires
    private IDataCache pCache;

    /** ERP Proxy (imported service) */
    @Requires(id = IPOJO_ID_ERP, optional = true)
    private IErpDataProxy pErpProxy;

    /** Log service */
    @Requires
    private IIsolateLoggerSvc pLogger;

    /**
     * Default constructor
     */
    public QuarterbackSvc() {

        super();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.psem2m.demo.erp.api.services.IErpData#applyCart(org.psem2m.demo.erp
     * .api.beans.CCartLine[])
     */
    @Override
    public CErpActionReport applyCart(final CCartLine[] aCartLines) {

        StringBuilder wSB = new StringBuilder();

        wSB.append(String.format("Received cart : [%d] lines : [ ",
                aCartLines.length));
        int wI = 0;
        for (CCartLine wCartLine : aCartLines) {
            if (wI > 0) {
                wSB.append(',');
            }
            wSB.append(String.format("{%d=>%s }", wI, wCartLine.toString()));
            wI++;
        }
        wSB.append("]");

        return new CErpActionReport(
                java.net.HttpURLConnection.HTTP_NOT_IMPLEMENTED,
                "applyCart() must be implemented.", wSB.toString());
    }

    /**
     * Called by iPOJO when the ERP proxy is bound
     * 
     * @param aErpDataProxy
     *            The ERP Proxy service
     */
    @Bind(id = IPOJO_ID_ERP)
    protected void bindErp(final IErpDataProxy aErpDataProxy) {

        // Update the flag
        isProxyAvailable = true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.demo.erp.api.services.IErpData#getItem(java.lang.String)
     */
    @Override
    public CachedItemBean getItem(final String aItemId) {

        if (isProxyAvailable) {
            // Try with the ERP

            try {
                final ItemBean itemBean = pErpProxy.getItem(aItemId);

                if (itemBean != null) {
                    // Update the cache
                    pCache.updateItemBean(aItemId, itemBean);

                    // Return the bean
                    return new CachedItemBean(itemBean,
                            IQualityLevels.CACHE_LEVEL_SYNC);
                }

            } catch (Exception e) {
                // An error occurred using the ERP, continue with the cache
            }
        }

        // The ERP failed
        synchronized (pCache) {

            final ItemBean item;
            final String itemId;

            if ("?".equals(aItemId)) {
                item = pCache.getRandomItem();
                if (item != null) {
                    itemId = item.getId();
                } else {
                    itemId = null;
                }

            } else {
                item = pCache.getItem(aItemId);
                itemId = aItemId;
            }

            final long itemAge = pCache.getItemInformationAge(itemId);

            if (item != null && itemAge != -1) {
{'fraction_non_alphanumeric': 0.111869918699187, 'fraction_numerical': 0.004986449864498645, 'mean_word_length': 2.279772484891575, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6350942', 'n_tokens_mistral': 3038, 'n_tokens_neox': 2905, 'n_words': 557}