diff --git a/src/model/Movie.java b/src/model/Movie.java index 36442d4..1e6dbb8 100644 --- a/src/model/Movie.java +++ b/src/model/Movie.java @@ -1,5 +1,9 @@ package model; +import java.lang.reflect.InvocationTargetException; + +import org.apache.commons.beanutils.BeanUtils; + public class Movie implements Comparable{ //MovieID::Title::Genres private Integer id; @@ -53,4 +57,15 @@ public String toString() { return this.id+"|"+this.title+"|"+this.genres+"|"+this.rated; } + public Movie deepCopy(){ + try { + Movie copy = (Movie) BeanUtils.cloneBean(this); + return copy; + } catch (IllegalAccessException | InstantiationException | InvocationTargetException + | NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } } diff --git a/src/recommend/Recommend.java b/src/recommend/Recommend.java index 41de02d..9827516 100644 --- a/src/recommend/Recommend.java +++ b/src/recommend/Recommend.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; import java.util.List; import javax.servlet.ServletException; @@ -39,7 +40,13 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t List rec =null; if("recommend".equals(flag)){ recNum = request.getParameter("recommendNum"); - rec= Utils.predict(Integer.parseInt(uid),Integer.parseInt(recNum)); + try { + rec= Utils.predict(Integer.parseInt(uid),Integer.parseInt(recNum)); + } catch (NumberFormatException | IllegalAccessException | InstantiationException | InvocationTargetException + | NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } }else{ rec = Utils.check(Integer.parseInt(uid)); } diff --git a/src/util/Utils.java b/src/util/Utils.java index ea10835..4c0d8d0 100644 --- a/src/util/Utils.java +++ b/src/util/Utils.java @@ -3,12 +3,14 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; @@ -171,8 +173,12 @@ public static String readHDFS(String outputPath) { * @param uid * @param recNum * @return + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws InstantiationException + * @throws IllegalAccessException */ - public static List predict(int uid,int recNum) { + public static List predict(int uid,int recNum) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { if (userFeatures.size() <= 0 || productFeatures.size() <= 0) { try { userFeatures = getModelFeatures(userFeaturePath); @@ -198,7 +204,7 @@ public static List predict(int uid,int recNum) { double score = 0.0; BLAS blas = BLAS.getInstance(); for (int candidate : candidates) { - movie = movies.get(candidate); + movie = movies.get(candidate).deepCopy(); pFeature = productFeatures.get(candidate); if (pFeature == null) continue; @@ -339,7 +345,7 @@ public static void init() throws IOException { private static Map userFeatures = new HashMap<>(); private static Map productFeatures = new HashMap<>(); - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { init(); int uid = 1;