<commit_before>/**
 * File:   ErpCaller.java
 * Author: <NAME>
 * Date:   14 nov. 2011
 */
package org.psem2m.composer.demo.impl.applyCart;

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

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Property;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.framework.BundleException;
import org.psem2m.composer.demo.DemoComponentsConstants;
import org.psem2m.composer.test.api.IComponent;
import org.psem2m.composer.test.api.IComponentContext;
import org.psem2m.isolates.base.IIsolateLoggerSvc;
import org.psem2m.isolates.base.activators.CPojoBase;

/**
 * applyCart result normalizer
 * 
 * @author <NAME>
 */
@Component(name = DemoComponentsConstants.COMPONENT_NORMALIZER_APPLYCART)
@Provides(specifications = IComponent.class)
public class NormalizerApplyCart extends CPojoBase implements IComponent {

    /** The logger */
    @Requires
    private IIsolateLoggerSvc pLogger;

    /** The instance name */
    @Property(name = DemoComponentsConstants.PROPERTY_INSTANCE_NAME)
    private String pName;

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.composer.test.api.IComponent#computeResult(java.util.Map)
     */
    @Override
    public IComponentContext computeResult(final IComponentContext aContext)
            throws Exception {

        if (aContext.hasError()) {
            // Prepare a new map, with both result and error
            final Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put(KEY_ERROR, aContext.getErrors().toArray());
            resultMap.put(KEY_RESULT, aContext.getResults().toArray());

            aContext.setResult(resultMap);
            return aContext;
        }

        if (!aContext.hasResult()) {
            // No error and no result...
            aContext.addError(pName, "No result found...");
            return aContext;
        }

        return aContext;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.isolates.base.activators.CPojoBase#invalidatePojo()
     */
    @Override
    @Invalidate
    public void invalidatePojo() throws BundleException {

        pLogger.logInfo(this, "invalidatePojo", "Component", pName, "Gone");
    }

    /*
     * (non-Javadoc)
     * <commit_msg>Modification du normalizer 'NormalizerApplyCart' du canal 'ApplyCart' pour mettre les exceptions dans le format 'message+reason+code' attendu pr le WebStore<commit_after>/**
 * File:   ErpCaller.java
 * Author: <NAME>
 * Date:   14 nov. 2011
 */
package org.psem2m.composer.demo.impl.applyCart;

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

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Property;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.framework.BundleException;
import org.psem2m.composer.demo.DemoComponentsConstants;
import org.psem2m.composer.test.api.IComponent;
import org.psem2m.composer.test.api.IComponentContext;
import org.psem2m.isolates.base.IIsolateLoggerSvc;
import org.psem2m.isolates.base.activators.CPojoBase;

/**
 * applyCart result normalizer
 * 
 * @author <NAME>
 */
@Component(name = DemoComponentsConstants.COMPONENT_NORMALIZER_APPLYCART)
@Provides(specifications = IComponent.class)
public class NormalizerApplyCart extends CPojoBase implements IComponent {

    /** The logger */
    @Requires
    private IIsolateLoggerSvc pLogger;

    /** The instance name */
    @Property(name = DemoComponentsConstants.PROPERTY_INSTANCE_NAME)
    private String pName;

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.composer.test.api.IComponent#computeResult(java.util.Map)
     */
    @Override
    public IComponentContext computeResult(final IComponentContext aContext)
            throws Exception {

        if (aContext.hasError()) {
            // Prepare a new map, with 'message' 'reason' and 'code'
            final Map<String, Object> resultMap = new HashMap<String, Object>();

            /*
             * 
             * array ( 'status' => 500, 'message' =>
             * 'DataServerApplication.CartsApplier2.safeErpCaller : Exception
             * caught class=[org.jabsorb.client.ErrorResponse] mess(1)=[JSONRPC
             * error code -32603: | ErrorResponse |
             * org.jabsorb.client.Client(processException:227)]
             * stack=[org.jabsorb.client.Client(processException:227)
             * org.jabsorb.client.Client(invoke:174)
             * org.jabsorb.client.Client(invoke:135) $Proxy1(applyCart:-1)
             * org.psem2m.composer.demo.erpproxy.ErpProxy(__M_applyCart:92)
             * org.psem2m.composer.demo.erpproxy.ErpProxy(applyCart:-1)
             * org.psem2m.composer.demo.erpproxy.ErpProxy(__M_computeResult:119)
             * org.psem2m.composer.demo.erpproxy.ErpProxy(computeResult:-1)
             */
            String wMessage = "no message !";
            String wReason = "no reason !";
            if (aContext.getErrors().size() > 0) {
                String wInfos = aContext.getErrors().get(0);
                String[] wParts = wInfos.split("\\|");
                if (wParts != null && wParts.length >= 2) {
                    wReason = wParts[0];
                    wMessage = wParts[1];
                }
            }
            resultMap.put("message", wMessage);
            resultMap.put("reason", wReason);
            resultMap.put("code", 500);

            aContext.setResult(resultMap);
            return aContext;
        }

        if (!aContext.hasResult()) {
            // No error and no result...
            aContext.addError(pName, "No result found...");
            return aContext;
        }

        return aContext;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.psem2m.isolates.base.activators.CPojoBase#invalidatePojo()
     */
    @Override
    @Invalidate
    public void invalidatePojo() throws BundleException {

        pLogger.logInfo(this, "invalidatePojo", "Component", pName, "Gone");
    }

    /*
     * (non-Javadoc)
     * 
{'fraction_non_alphanumeric': 0.11696637998436278, 'fraction_numerical': 0.011102423768569195, 'mean_word_length': 2.705677867902665, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '5330925', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1996, 'n_words': 340}