Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Username #12

Merged
merged 7 commits into from
Dec 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions app/controllers/Application.java

Large diffs are not rendered by default.

323 changes: 317 additions & 6 deletions app/controllers/FilterDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import play.mvc.Controller;
import play.mvc.Result;
import views.html.productlist;
import views.html.userproductlist;

public class FilterDB extends Controller {
private static String db_url = "jdbc:postgresql://cloudproj.ct233hyipvfx.us-east-1.rds.amazonaws.com:5432/elandb";
Expand Down Expand Up @@ -125,7 +126,7 @@ public static Result getProductByProdTypeAndRatingWithoutColor(
}
return ok(productlist.render(prodList));
}

public static Result getProductByProdTypeAndRatingWithUserName(
int page_num, int prod_type, String username) throws Exception {

Expand All @@ -141,12 +142,13 @@ public static Result getProductByProdTypeAndRatingWithUserName(
while (rs.next()) {
list.add(rs.getInt("color_id"));
}
List<Product> rlist=null;
if (list.size() == 0) {
getProductByProdTypeAndRatingWithoutColor(prod_type, page_num);
rlist=FilterDBReturnList.getProductByProdTypeAndRatingWithoutColor(prod_type, page_num);
} else {
getProductByProdTypeAndRatingWithColor(page_num, prod_type, list);
rlist=FilterDBReturnList.getProductByProdTypeAndRatingWithColor(page_num, prod_type, list);
}
return ok();
return ok( userproductlist.render(username,rlist));

}

Expand All @@ -169,11 +171,12 @@ public static Result getProductByMerchTypeAndRatingWithUserName(
merchant.add(merch);
List<Integer> dummy=new ArrayList<Integer>();
List<Combo> dummyc=new ArrayList<Combo>();
List<Product> rlist=null;
if (list.size() == 0) {
getProductByCompleteWithoutColor(page_num, dummy, merchant, dummyc,
rlist=getProductByCompleteWithoutColor(page_num, dummy, merchant, dummyc,
dummy);
} else {
getProductByForSideFilterWithColour(page_num, dummy, merchant,list,
rlist=FilterDBReturnList.getProductByForSideFilterWithColour(page_num, dummy, merchant,list,
dummyc,dummy);
}
return ok();
Expand Down Expand Up @@ -275,6 +278,9 @@ public static Result getProductByProdTypeAndRatingWithColor(int page_num,
return ok();
}




public static class Combo {
public Combo(Integer begin, Integer end) {
super();
Expand All @@ -286,6 +292,152 @@ public Combo(Integer begin, Integer end) {
public Integer end;
}

public static Result getAllProductsWithoutColorWithResult(int page_num,
List<Integer> prod_type, List<Integer> merchant, List<Combo> price,
List<Integer> rating)// , List<Integer> merchant, List<Combo>
// price,List<Integer> rating)
throws Exception {
List<Product> prodList = new ArrayList<Product>();
Connection conn = initializeConnection();
Statement statement = null;
ResultSet rs = null;
Connection connection = null;
int start = 12 * (page_num - 1) + 1, end = 12 * page_num;
boolean where = false;
String ptype = "";
if (prod_type.size() > 0) {
where = true;
ptype += prod_type.get(0);
for (int i = 1; i < prod_type.size(); i++)
ptype += "," + prod_type.get(i);
ptype += ")";
}
if (!ptype.equals(""))
ptype = " p.type_id IN (" + ptype;
String mtype = "";
if (merchant.size() > 0) {
where = true;
mtype += merchant.get(0);
for (int i = 1; i < merchant.size(); i++)
mtype += "," + merchant.get(i);
mtype += ")";
}
if (!mtype.equals(""))
mtype = " p.merchant_id IN (" + mtype;
if (!ptype.equals("") && !mtype.equals(""))
mtype = " and" + mtype;

String priceq = "";
if (price.size() > 0) {
where = true;
priceq += " (p.price BETWEEN " + price.get(0).begin + " AND "
+ price.get(0).end + ")";
}
for (int i = 1; i < price.size(); i++) {
priceq += " or ";
priceq += " (p.price BETWEEN " + price.get(i).begin + " AND "
+ price.get(i).end + ")";
}
if (!priceq.equals(""))
priceq = "(" + priceq + ")";
if (!mtype.equals("") && !priceq.equals(""))
priceq = " and" + priceq;
System.out.println(priceq);
String ratingq = "";
if (rating.size() > 0) {
where = true;
ratingq += "(p.rating >= " + rating.get(0) + ")";
}
for (int i = 1; i < rating.size(); i++) {
ratingq += " or ";
ratingq += "(p.rating >=" + rating.get(i) + ")";
}
if (!ratingq.equals(""))
ratingq = "(" + ratingq + ")";
if (!priceq.equals("") && !ratingq.equals(""))
ratingq = " and" + ratingq;
System.out.println(ratingq);

String stmt = "CREATE OR REPLACE FUNCTION RecoByWOC(prod_type1 integer) "

+ " returns TABLE ( rn bigint,id integer,merchant_name character varying(255),ptype integer,price double precision,rating double precision,image bytea) as $$ begin "
+ " return query "
+ "select * from (select row_number() over() rn,others.id,others.merchant_name,others.ptype,others.price,others.rating,im.image from images im inner join (select p.id as id,m.name as merchant_name,p.price as price,p.rating as rating,p.type_id as ptype"
+ " from products p inner join merchant m on p.merchant_id=m.merchant_id "
+ (where ? " where " : "")
+ ptype
+ mtype
+ priceq
+ ratingq
+ " order by p.rating desc limit "
+ 12
* page_num
+ " ) as others on im.id=others.id) as temp where temp.rn>="
+ start
+ " and temp.rn<=+"
+ end
+ "; end;"
+ " $$ LANGUAGE 'plpgsql' IMMUTABLE SECURITY DEFINER COST 10 ";
System.out.println(stmt);
//
try {
connection = conn;

statement = connection.createStatement();
statement.execute(stmt);
statement.close();

PreparedStatement cstmt = connection
.prepareCall("{call RecoByWOC(?)}");

cstmt.setInt(1, 1); // The name argument is the second ?

cstmt.execute();

ResultSet set = ((ResultSet) cstmt.getResultSet());
int i = 1;
while (set.next()) {
byte[] b = set.getBytes("image");
Product product = new Product(set.getInt("id"),
set.getFloat("price"), set.getFloat("rating"),
new String(Base64.encodeBase64(b)),
set.getString("merchant_name"));
System.out.println(i++ + product.toString());
prodList.add(product);
}

cstmt.close();

} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return ok();
}



public static List<Product> getAllProductsWithoutColor(int page_num,
List<Integer> prod_type, List<Integer> merchant, List<Combo> price,
List<Integer> rating)// , List<Integer> merchant, List<Combo>
Expand Down Expand Up @@ -587,6 +739,165 @@ public static List<Product> getProductByCompleteWithoutColor(int page_num,
return prodList;
}


public static Result getProductByCompleteWithoutColorWithResult(int page_num,
List<Integer> prod_type, List<String> merch, List<Combo> price,
List<Integer> rating)// , List<Integer> merchant, List<Combo>
// price,List<Integer> rating)
throws Exception {
List<Product> prodList = new ArrayList<Product>();
Connection conn = initializeConnection();
Statement statement = null;
ResultSet rs = null;
Connection connection = null;
int start = 12 * (page_num - 1) + 1, end = 12 * page_num;
boolean where = false;
List<Integer> merchant = new ArrayList<Integer>();
for (String m : merch) {
String merch_st = "select merchant_id from merchant where name='"
+ m + "'";
statement = conn.createStatement();
rs = statement.executeQuery(merch_st);

while (rs.next()) {
merchant.add(rs.getInt("merchant_id"));

}
}
String ptype = "";
if (prod_type.size() > 0) {
where = true;
ptype += prod_type.get(0);
for (int i = 1; i < prod_type.size(); i++)
ptype += "," + prod_type.get(i);
ptype += ")";
}
if (!ptype.equals(""))
ptype = " p.type_id IN (" + ptype;
String mtype = "";
if (merchant.size() > 0) {
where = true;
mtype += merchant.get(0);
for (int i = 1; i < merchant.size(); i++)
mtype += "," + merchant.get(i);
mtype += ")";
}
if (!mtype.equals(""))
mtype = " p.merchant_id IN (" + mtype;
if (!ptype.equals("") && !mtype.equals(""))
mtype = " and" + mtype;

String priceq = "";
if (price.size() > 0) {
where = true;
priceq += " (p.price BETWEEN " + price.get(0).begin + " AND "
+ price.get(0).end + ")";
}
for (int i = 1; i < price.size(); i++) {
priceq += " or ";
priceq += " (p.price BETWEEN " + price.get(i).begin + " AND "
+ price.get(i).end + ")";
}
if (!priceq.equals(""))
priceq = "(" + priceq + ")";
if (!mtype.equals("") && !priceq.equals(""))
priceq = " and" + priceq;
System.out.println(priceq);
String ratingq = "";
if (rating.size() > 0) {
where = true;
ratingq += "(p.rating >= " + rating.get(0) + ")";
}
for (int i = 1; i < rating.size(); i++) {
ratingq += " or ";
ratingq += "(p.rating >=" + rating.get(i) + ")";
}
if (!ratingq.equals(""))
ratingq = "(" + ratingq + ")";
if (!priceq.equals("") && !ratingq.equals(""))
ratingq = " and" + ratingq;
System.out.println(ratingq);

String stmt = "CREATE OR REPLACE FUNCTION RecoByWOC(prod_type1 integer) "

+ " returns TABLE ( rn bigint,id integer,merchant_name character varying(255),ptype integer,price double precision,rating double precision,image bytea) as $$ begin "
+ " return query "
+ "select * from (select row_number() over() rn,others.id,others.merchant_name,others.ptype,others.price,others.rating,im.image from images im inner join (select p.id as id,m.name as merchant_name,p.price as price,p.rating as rating,p.type_id as ptype"
+ " from products p inner join merchant m on p.merchant_id=m.merchant_id "
+ (where ? " where " : "")
+ ptype
+ mtype
+ priceq
+ ratingq
+ " order by p.rating desc limit "
+ 12
* page_num
+ " ) as others on im.id=others.id) as temp where temp.rn>="
+ start
+ " and temp.rn<=+"
+ end
+ "; end;"
+ " $$ LANGUAGE 'plpgsql' IMMUTABLE SECURITY DEFINER COST 10 ";
System.out.println(stmt);
//
try {
connection = conn;

statement = connection.createStatement();
statement.execute(stmt);
statement.close();

PreparedStatement cstmt = connection
.prepareCall("{call RecoByWOC(?)}");

cstmt.setInt(1, 1); // The name argument is the second ?

cstmt.execute();

ResultSet set = ((ResultSet) cstmt.getResultSet());
int i = 1;
while (set.next()) {
byte[] b = set.getBytes("image");
Product product = new Product(set.getInt("id"),
set.getFloat("price"), set.getFloat("rating"),
new String(Base64.encodeBase64(b)),
set.getString("merchant_name"));
System.out.println(i++ + product.toString());
prodList.add(product);
}

cstmt.close();

} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return ok();
}



public static Result getProductByForSideFilterWithColour(int page_num,
List<Integer> prod_type, List<String> merch, List<Integer> color,
List<Combo> price, List<Integer> rating)// , List<Integer> merchant,
Expand Down
Loading