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

Java tutorial

Introduction

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

Source

/**
 * PureInfo TGirls
 * @(#)GetPicArrServlet.java   1.0 2009-3-10
 * 
 * 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.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.tfr.cache.core.CacheManager;
import com.pureinfo.tfr.cache.core.IReadCache;
import com.pureinfo.tgirls.domain.IPhotoMgr;
import com.pureinfo.tgirls.model.Photo;

public class GetPic {

    private static Logger logger = Logger.getLogger(GetPic.class);

    static IReadCache cache = CacheManager.getReadFromDBCache("view.pics");

    public static Photo get(String type, int photoId) {

        List<Photo> cacheList = null;

        logger.debug("get pic whit type[" + type + "] id[" + photoId + "] ");

        if (photoId <= 0) {
            return getRandomPic(type);
        }

        cacheList = (List<Photo>) cache.get("pics." + type);
        if (cacheList != null) {

            logger.debug("try to get from cache.");

            Photo p;
            for (Iterator iterator = cacheList.iterator(); iterator.hasNext();) {
                p = (Photo) iterator.next();
                if (p.getId() == photoId) {
                    return p;
                }
            }
        }

        logger.debug("load pic from DB.");
        IPhotoMgr mgr;
        try {
            mgr = (IPhotoMgr) ArkContentHelper.getContentMgrOf(Photo.class);
            Photo p = (Photo) mgr.lookupById(photoId);
            if (p == null) {
                return null;
            }
            List<Photo> buylist, uploadList;
            buylist = mgr.getUserBuyPublicPics(p.getBuyerTaobaoId());
            uploadList = mgr.getUserUploadPublicPics(p.getUploaderTaobaoId());

            p.setBuyerBuyPics(buylist);
            p.setUploaderUploadPics(uploadList);
            return p;
        } catch (Exception e) {
            logger.equals(e);
        }

        return null;
    }

    public static List<Photo> getPicList(String type) {
        List<Photo> cacheList = null;

        if ((cacheList = (List<Photo>) cache.get("pics." + type)) == null) {

            logger.debug("the cache list is null. reload.");

            try {

                IPhotoMgr mgr = (IPhotoMgr) ArkContentHelper.getContentMgrOf(Photo.class);
                if (StringUtils.isEmpty(type)) {
                    cacheList = mgr.getRandomPics(300);
                } else {
                    cacheList = mgr.getRandomPics(Integer.parseInt(type), 300);
                }
                if (cacheList != null) {
                    Photo photo;
                    List<Photo> buylist, uploadList;
                    for (Iterator iterator = cacheList.iterator(); iterator.hasNext();) {
                        photo = (Photo) iterator.next();
                        buylist = mgr.getUserBuyPublicPics(photo.getBuyerTaobaoId());
                        uploadList = mgr.getUserUploadPublicPics(photo.getUploaderTaobaoId());

                        photo.setBuyerBuyPics(buylist);
                        photo.setUploaderUploadPics(uploadList);
                    }

                    cache.put("pics." + type, cacheList);

                }

            } catch (PureException e) {
                logger.error("error when get pics.", e);
            }
        }

        return cacheList;
    }

    public static Photo getRandomPic(String type) {

        logger.debug("to get pics with type:" + type);

        List<Photo> cacheList = null;

        if ((cacheList = (List<Photo>) cache.get("pics." + type)) == null) {

            try {

                IPhotoMgr mgr = (IPhotoMgr) ArkContentHelper.getContentMgrOf(Photo.class);
                if (StringUtils.isEmpty(type)) {
                    cacheList = mgr.getRandomPics(300);
                } else {
                    cacheList = mgr.getRandomPics(Integer.parseInt(type), 300);
                }
                if (cacheList != null) {
                    Photo photo;
                    List<Photo> buylist, uploadList;
                    for (Iterator iterator = cacheList.iterator(); iterator.hasNext();) {
                        photo = (Photo) iterator.next();
                        buylist = mgr.getUserBuyPublicPics(photo.getBuyerTaobaoId());
                        uploadList = mgr.getUserUploadPublicPics(photo.getUploaderTaobaoId());

                        photo.setBuyerBuyPics(buylist);
                        photo.setUploaderUploadPics(uploadList);
                    }

                    cache.put("pics." + type, cacheList);
                }

            } catch (PureException e) {
                logger.error("error when get pics.", e);
            }
        }

        if (cacheList == null || cacheList.isEmpty()) {
            return null;
        }

        int index = new Random().nextInt(cacheList.size());

        return cacheList.get(index);
    }

    public static Photo getPic(String _type, List<String> _exincludeIds) {
        List<Photo> cacheList = GetPic.getPicList(_type);

        if (cacheList == null) {
            return null;
        }

        Photo p = null;
        for (int i = 0; i < cacheList.size(); i++) {
            p = cacheList.get(i);
            if (_exincludeIds.contains(String.valueOf(p.getId()))) {
                logger.debug("id[" + p.getId() + "] exists.");
                continue;
            } else {

                logger.debug("add id[" + p.getId() + "]");

                _exincludeIds.add(String.valueOf(p.getId()));
                return p;
            }

        }

        int i = new Random().nextInt(cacheList.size());

        logger.debug("all ids exists, get random one: " + i + "/" + cacheList.size());

        return cacheList.get(i);
    }

    public static String idListToString(List<String> _viewdPicIds) {
        if (_viewdPicIds == null) {
            return "";
        }
        StringBuffer sbuff = new StringBuffer();
        for (Iterator iterator = _viewdPicIds.iterator(); iterator.hasNext();) {
            String string = (String) iterator.next();

            if (sbuff.length() == 0) {
                sbuff.append(string);
            } else {
                sbuff.append(",").append(string);
            }
        }
        return sbuff.toString();
    }
}