outfox.dict.contest.test.InitSingerData.java Source code

Java tutorial

Introduction

Here is the source code for outfox.dict.contest.test.InitSingerData.java

Source

/**
 * @(#)InitSingerData.java, 2016-3-5. 
 * 
 * Copyright 2016 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package outfox.dict.contest.test;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import outfox.dict.contest.dao.ContestDAO;
import outfox.dict.contest.data.SingerEntity;
import outfox.dict.contest.front.data.ContestConsts;
import outfox.dict.contest.front.data.Result;
import outfox.dict.contest.util.HttpToolKit;

/**
 *
 * @author wangning
 *
 */
@Controller
public class InitSingerData {

    @Autowired
    private ContestDAO contestDAO;

    @ResponseBody
    @RequestMapping(value = "/initData")
    public String initData() {
        SingerEntity singerEntity = new SingerEntity();

        // 10000??
        for (int i = 1; i <= 10; i++) {
            // ????
            singerEntity.setUserId(String.valueOf(i));
            singerEntity.setName("" + i);
            singerEntity.setSchool(getRandomSchool());
            singerEntity.setAreaName(getRandomCity());
            singerEntity.setPhotoUrl("http://p1.music.126.net/aHW-oDNmj4FcuB34vmg4Ng==/2544269906854087.jpg");
            singerEntity.setAudioUrl("http://m2.music.126.net/S7r4Uu8mPCwNhvHcfVbtEA==/2029698464885626.mp3");
            singerEntity.setAge(18);
            singerEntity.setSex(false);
            singerEntity.setMusicName("" + i);
            singerEntity.setPhone("121221" + i);

            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            singerEntity.setSingupTime(timestamp);

            // 1: ?
            contestDAO.signup(singerEntity);

            // 2: ??
            String content = "{\"id\": " + singerEntity.getId() + ", \"name\": \"" + singerEntity.getName()
                    + "\", \"school\": \"" + singerEntity.getSchool() + "\", \"areaName\": \""
                    + singerEntity.getAreaName() + "\", \"audioUrl\": \"" + singerEntity.getAudioUrl() + "\"}";
            HttpResponse response = null;
            try {
                List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
                paramsList.add(new BasicNameValuePair("voteId", ContestConsts.VOTE_ID));
                paramsList.add(new BasicNameValuePair("content", content));
                paramsList.add(new BasicNameValuePair("picture", singerEntity.getPhotoUrl()));
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramsList, "UTF-8");
                response = HttpToolKit.getInstance().doPost(ContestConsts.ADD_VOTE_OPTION_INTERFACE, entity);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                HttpToolKit.closeQuiet(response);
            }
            System.out.println("*********" + i + "???*********");
        }
        return Result.SUCCESS.json(null, null);
    }

    /*
     * ??
     */
    public static String getRandomCity() {
        String[] arr = { "", "", "", "", "?", "" };
        Random rand = new Random();
        int index = rand.nextInt(arr.length);
        return arr[index];
    }

    /*
     * ????
     */
    public static String getRandomSchool() {
        String[] arr = { "?", "", "", "", "?",
                "?", "" };
        Random rand = new Random();
        int index = rand.nextInt(arr.length);
        return arr[index];
    }
}