Subversion Repositories SmartDukaan

Rev

Rev 2620 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright 2010 Google Inc.
 *
 * 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 in.shop2020.web.mappers;

import java.util.List;
import java.util.logging.Logger;

import org.apache.hadoop.io.NullWritable;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.tools.mapreduce.AppEngineMapper;

/**
 * A sample AppEngine mapper.
 * 
 * @author frew@google.com (Fred Wulff)
 */
public class TestMapper extends
        AppEngineMapper<Key, Entity, NullWritable, NullWritable> {
    private static final Logger log = Logger.getLogger(TestMapper.class
            .getName());

    public TestMapper() {
    }

    @Override
    public void taskSetup(Context context) {
        log.warning("Doing per-task setup");
    }

    @Override
    public void taskCleanup(Context context) {
        log.warning("Doing per-task cleanup");
    }

    @Override
    public void setup(Context context) {
        log.warning("Doing per-worker setup");
    }

    @Override
    public void cleanup(Context context) {
        log.warning("Doing per-worker cleanup");
    }

    // This is a silly mapper that's intended to show some of the capabilities
    // of the API.
    @Override
    public void map(Key key, Entity value, Context context) {
        countItemEvent("ADD_TO_CART", key, value, context);
        countItemEvent("DELETE_FROM_CART", key, value, context);
        countProductEvent("PRODUCT_VIEW", key, value, context);
        countItemEvent("PRODUCT_COMPARE", key, value, context);
        countItemEvent("RESEARCH_ADD", key, value, context);
        countItemEvent("RESEARCH_DELETE", key, value, context);
    }

    private void countProductEvent(String eventType, Key key, Entity value, Context context) {
        if (value.hasProperty("eventType") && value.getProperty("eventType")!=null) {
            if (value.getProperty("eventType").toString().equals(eventType)) {
                    String productName = value.getProperty("productName").toString();
                    context.getCounter(productName, eventType).increment(1);
            }
        }
    }

    @SuppressWarnings("unchecked")
    private void countItemEvent(String eventType, Key key, Entity value,
            Context context) {
        if (value.hasProperty("eventType") && value.getProperty("eventType")!=null) {
            if (value.getProperty("eventType").toString().equals(eventType)) {
                try {
                    List<Long> entityIds = (List<Long>)value.getProperty("itemIds");
                    for (Long entityId : entityIds) {
                        context.getCounter(entityId.toString(), eventType).increment(1);
                    }
                }
                catch (ClassCastException e) {
                    try {
                        Long entityId = (Long) value.getProperty("itemIds");
                        context.getCounter(entityId.toString(), eventType).increment(1);
                    }
                    catch (ClassCastException e1) {
                        log.warning("Unable to cast entityIds to List of long or long.");
                    }
                }
            }
        }
    }
}