com.pureinfo.tgirls.servlet.OtherCompanyInitServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.tgirls.servlet.OtherCompanyInitServlet.java

Source

/**
 * PureInfo TGirls
 * @(#)MyCompanyInitServlet.java   1.0 2009-4-8
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.tgirls.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.tgirls.domain.IPhotoMgr;
import com.pureinfo.tgirls.domain.IUserMgr;
import com.pureinfo.tgirls.json.model.JsonBase;
import com.pureinfo.tgirls.model.Photo;
import com.pureinfo.tgirls.model.User;

public class OtherCompanyInitServlet extends HttpServlet {
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 1L;

    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        JsonBase result = new JsonBase();
        response.setContentType("text/json; charset=utf-8");
        String userId = request.getParameter("uid");
        if (StringUtils.isEmpty(userId)) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }

        try {
            IUserMgr umgr = (IUserMgr) ArkContentHelper.getContentMgrOf(User.class);
            User currentUser = umgr.getUserByTaobaoId(userId);
            if (currentUser == null) {
                response.sendRedirect(request.getContextPath() + "/tgirls/need-login.html");
                return;
            }

            logger.info("other company init with user[" + currentUser.getTaobaoID() + "]");

            IPhotoMgr mgr = (IPhotoMgr) ArkContentHelper.getContentMgrOf(Photo.class);

            List<Photo> uploadlist = mgr.getUserUploadPublicPics(currentUser.getTaobaoID());
            if (uploadlist != null) {
                List<JSONObject> pjsonlist = new ArrayList<JSONObject>();
                Photo object = null;
                for (Iterator iterator = uploadlist.iterator(); iterator.hasNext();) {
                    object = (Photo) iterator.next();
                    pjsonlist.add(new JSONObject(object));
                }

                logger.debug("uploads:" + pjsonlist.size());

                result.put("uploads", pjsonlist);

                uploadlist.clear();
            }

            List<Photo> buylist = mgr.getUserBuyPublicPics(currentUser.getTaobaoID());
            if (buylist != null) {
                List<JSONObject> pjsonlist = new ArrayList<JSONObject>();
                Photo object = null;
                for (Iterator iterator = buylist.iterator(); iterator.hasNext();) {
                    object = (Photo) iterator.next();
                    pjsonlist.add(new JSONObject(object));
                }

                logger.debug("buys:" + pjsonlist.size());

                result.put("buys", pjsonlist);
                buylist.clear();
            }

            //            List<JSONObject> infolist = GetInformationsServlet.getInformations(currentUser.getTaobaoID());
            //            if (infolist != null) {
            //                result.put("informations", infolist);
            //            }
            result.put("uinfo", new JSONObject(currentUser));
        } catch (JSONException e) {
            logger.error("json error", e);
        } catch (PureException e) {
            logger.error("ark error", e);
        }

        // logger.debug("result:" + result.toString());
        response.getWriter().write(result.toString());
        return;
    }

    @Override
    protected void doPost(HttpServletRequest _req, HttpServletResponse _resp) throws ServletException, IOException {
        this.doGet(_req, _resp);
    }

}