Skip to content

Instantly share code, notes, and snippets.

@mdashti
Created May 11, 2014 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdashti/a47bd9328bebfa6babdf to your computer and use it in GitHub Desktop.
Save mdashti/a47bd9328bebfa6babdf to your computer and use it in GitHub Desktop.
package com.example.controller;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import javassist.expr.NewArray;
import org.apache.commons.io.input.SwappedDataInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.model.Person;
import com.example.model.Tag_DB;
import com.example.model.UserDB;
import com.example.model.wishDB;
import com.example.service.Create_Wish_Services;
import com.example.service.PersonService;
import com.example.service.Tag_Service;
import com.example.service.User_Service;
@Controller
@RequestMapping("/test")
public class FBsantaController {
@Autowired
private Create_Wish_Services wish_serv;
@Autowired
private Tag_Service tag_serv;
@Autowired
private User_Service user_serv;
// public FBsantaController()//(Create_Wish_Services mywith_serv, Tag_Service mytag_serv, User_Service myuser_serv) {
// this.wish_serv = mywith_serv;
// this.tag_serv = mytag_serv;
// this.user_serv = myuser_serv;
// }
@RequestMapping(value="/add_samples")//, method = RequestMethod.GET) // Default
public @ResponseBody Map<String, Object> add_samples() {
Map<String, Object> result = new HashMap<String, Object>();
wishDB myw = new wishDB();
myw.setAge(21);
myw.setDate(new Date());
wish_serv.Add_Wish(myw);
result.put("success", true);
result.put("ashkan msg", myw.getAge());
result.put("Size", new Integer(wish_serv.All_wishes().size()));
return result;
}
@RequestMapping(value="/")//, method = RequestMethod.GET) // Default
public @ResponseBody Map<String, Boolean> getNewForm() {
Map<String, Boolean> result = new HashMap<String, Boolean>();
result.put("success", true);
return result;
}
@RequestMapping(value="/add_wish", method = RequestMethod.POST) // Wish Promotion
public @ResponseBody Map<String, Boolean> add_wish(@RequestParam String proposer_ID, @RequestParam String name, @RequestParam Integer age,
@RequestParam String location, @RequestParam Integer gender, @RequestParam String good_to_know,
@RequestParam String person_picture, @RequestParam String title, @RequestParam String wish_tags,
@RequestParam String description) {
Map<String, Boolean> result = new HashMap<String, Boolean>();
wishDB mywish = new wishDB(proposer_ID, name, age,
location, gender, good_to_know,
person_picture, title, wish_tags,
description, "", 0,
0, 0, new Date());
wish_serv.Add_Wish(mywish);
result.put("success", true);
return result;
}
@RequestMapping(value="/promotion/{wish_id}", method = RequestMethod.GET) // Wish Promotion
public @ResponseBody Map<String, Boolean> promotion(@PathVariable Integer wish_id) {
Map<String, Boolean> result = new HashMap<String, Boolean>();
wish_serv.Promote_Wish(wish_id);
result.put("success", true);
return result;
}
@RequestMapping(value="/status/{wish_id}", method = RequestMethod.GET) // Change status to Done (1)
public @ResponseBody Map<String, Boolean> status(@PathVariable Integer wish_id) {
Map<String, Boolean> result = new HashMap<String, Boolean>();
wish_serv.Change_Status(wish_id);
result.put("success", true);
return result;
}
//1 -> promotion 2->time
@RequestMapping(value="/retrieval_wish_based_nothing/{sort_key}/{start}/{count}", method = RequestMethod.GET) // Retrieval portion of all wishes based on Time or Promotion scores
public @ResponseBody Map<String, Object> retrieval_wish_based_nothing(@PathVariable Integer sort_key, @PathVariable Integer start, @PathVariable Integer count) {
Map<String, Object> result = new HashMap<String, Object>();
ArrayList<wishDB> wishes_order = new ArrayList<wishDB>();
List<wishDB> wishes;// = new ArrayList<wishDB>();
wishes = wish_serv.All_wishes();
for ( int i = 0; i < wishes.size(); i++)
for (int j = i+1; j < wishes.size(); j++)
{
if ( sort_key.intValue() == 1)
if ( wishes.get(i).getPromotion_counter() < wishes.get(j).getPromotion_counter() )
{
wishDB temp = new wishDB();
temp = wishes.get(i);
wishes.set(i, wishes.get(j));
wishes.set(i, temp);
}
if ( sort_key.intValue() == 2)
if ( wishes.get(i).getDate().before( wishes.get(j).getDate() ) )
{
wishDB temp = new wishDB();
temp = wishes.get(i);
wishes.set(i, wishes.get(j));
wishes.set(i, temp);
}
}
for ( int i = start; i < wishes.size() && i < start+count ; i++)
wishes_order.add(wishes.get(i));
result.put("success", true);
result.put("output", wishes_order);
return result;
}
//1 -> promotion 2->time
@RequestMapping(value="/retrieval_wish_based_tag/{tag}/{sort_key}/{start}/{count}", method = RequestMethod.GET) // Retrieval portion of filtered wishes based on Time or Promotion scores
public @ResponseBody Map<String, Object> retrieval_wish_based_tag( @PathVariable String tag, @PathVariable final Integer sort_key, @PathVariable Integer start, @PathVariable Integer count) {
Map<String, Object> result = new HashMap<String, Object>();
List<wishDB> all_wishes = wish_serv.All_wishes(); // All wishes in the database
ArrayList<wishDB> filtered_wishes = new ArrayList<wishDB>(); // wishes filtered by the tag (all wishes have the tag)
ArrayList<wishDB> result_wishes = new ArrayList<wishDB>(); // the portion of sorted and filtered wishes
for ( int i = 0; i < all_wishes.size(); i++) {
if(Arrays.asList(all_wishes.get(i).getWish_tags().split("^")).contains(tag)) { // Hesam: check equality!
filtered_wishes.add(all_wishes.get(i));
}
}
Collections.sort(filtered_wishes, new Comparator<wishDB>() { // sort based on the key
@Override
public int compare(wishDB o1, wishDB o2) { // Hesam: should be tested, -1 or 1?
if(sort_key.intValue()==1){ // based on promotion
if(o1.getPromotion_counter().intValue() > o2.getPromotion_counter().intValue())
return +1;
else if(o1.getPromotion_counter().intValue() < o2.getPromotion_counter().intValue())
return -1;
else
return 0;
}else{ // based on time
if(o1.getDate().getTime() > o2.getDate().getTime())
return +1;
else if(o1.getDate().getTime() < o2.getDate().getTime())
return -1;
else
return 0;
}
}
});
for ( int i = start; i < filtered_wishes.size() && i < start+count ; i++)
result_wishes.add(filtered_wishes.get(i));
result.put("success", true);
result.put("output", result_wishes);
return result;
}
@RequestMapping(value="/retrieval_wish_based_random/{start}/{count}", method = RequestMethod.GET) // Retrieval portion of shuffled (random) wishes
public @ResponseBody Map<String, Object> retrieval_wish_based_random( @PathVariable Integer start, @PathVariable Integer count) {
Map<String, Object> result = new HashMap<String, Object>();
List<wishDB> all_wishes = wish_serv.All_wishes(); // All wishes in the database
ArrayList<wishDB> result_wishes = new ArrayList<wishDB>(); // the portion of shuffled wishes
Collections.shuffle(all_wishes);
for ( int i = start; i < all_wishes.size() && i < start+count ; i++)
result_wishes.add(all_wishes.get(i));
result.put("success", true);
result.put("output", result_wishes);
return result;
}
@RequestMapping(value="/retrieval_tag/{start}/{count}", method = RequestMethod.GET) // retrieval portion of tags sorted based on the repetition
public @ResponseBody Map<String, Object> retrieval_tag( @PathVariable Integer start, @PathVariable Integer count) {
Map<String, Object> result = new HashMap<String, Object>();
ArrayList<Tag_DB> wishes_order = new ArrayList<Tag_DB>();
// java.util.List<wishDB> wishes = new ArrayList<wishDB>();
java.util.List<Tag_DB> tags = new ArrayList<Tag_DB>();
// wishes = wish_serv.All_wishes();
tags = tag_serv.All_tags();
for ( int i = 0; i < tags.size(); i++)
for (int j = i+1; j < tags.size(); j++)
if ( tags.get(i).getRepetition() < tags.get(j).getRepetition() )
{
Tag_DB temp = new Tag_DB();
temp = tags.get(i);
tags.set(i,tags.get(j));
tags.set(i, temp);
}
for ( int i = start; i < tags.size() && i < start+count ; i++)
wishes_order.add(tags.get(i));
result.put("success", true);
result.put("output", wishes_order);
return result;
}
@RequestMapping(value="/retrieval_user/{start}/{count}", method = RequestMethod.GET)
public @ResponseBody Map<String, Object> retrieval_user( @PathVariable Integer start, @PathVariable Integer count) {
Map<String, Object> result = new HashMap<String, Object>();
ArrayList<UserDB> users_order = new ArrayList<UserDB>();
// java.util.List<wishDB> wishes = new ArrayList<wishDB>();
java.util.List<UserDB> user = new ArrayList<UserDB>();
// wishes = wish_serv.All_wishes();
user = user_serv.All_users();
for ( int i = 0; i < user.size(); i++)
for (int j = i+1; j < user.size(); j++)
if ( user.get(i).getNumber_Pro() < user.get(j).getNumber_Pro() )
{
UserDB temp = new UserDB();
temp = user.get(i);
user.set(i,user.get(j));
user.set(i, temp);
}
for ( int i = start; i < user.size() && i < start+count ; i++)
users_order.add(user.get(i));
result.put("success", true);
result.put("output", users_order);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment