doortts doortts 2017-07-16
favorite: Update when project/org name changed
@48c642117c8c709e8dc29eb5cc5273bef1d6575a
app/controllers/OrganizationApp.java
--- app/controllers/OrganizationApp.java
+++ app/controllers/OrganizationApp.java
@@ -349,6 +349,7 @@
         Organization original = Organization.find.byId(modifiedOrganization.id);
         original.updateWith(modifiedOrganization);
         UserApp.currentUser().updateFavoriteOrganization(modifiedOrganization);
+        FavoriteOrganization.updateFavoriteOrganization(modifiedOrganization);
 
         return redirect(routes.OrganizationApp.settingForm(modifiedOrganization.name));
     }
app/controllers/ProjectApp.java
--- app/controllers/ProjectApp.java
+++ app/controllers/ProjectApp.java
@@ -270,6 +270,7 @@
 
         saveProjectMenuSetting(updatedProject);
         UserApp.currentUser().updateFavoriteProject(updatedProject);
+        FavoriteProject.updateFavoriteProject(updatedProject);
 
         return redirect(routes.ProjectApp.settingForm(ownerId, updatedProject.name));
     }
app/models/FavoriteOrganization.java
--- app/models/FavoriteOrganization.java
+++ app/models/FavoriteOrganization.java
@@ -12,6 +12,7 @@
 import javax.persistence.Id;
 import javax.persistence.ManyToOne;
 import javax.persistence.OneToOne;
+import java.util.List;
 
 @Entity
 public class FavoriteOrganization extends Model {
@@ -34,4 +35,13 @@
 
         this.organizationName = organization.name;
     }
+
+    public static void updateFavoriteOrganization(Organization organization) {
+        List<FavoriteOrganization> organizationList = finder.where().eq("organization.id", organization.id).findList();
+        for(FavoriteOrganization favoriteOrganization: organizationList){
+            favoriteOrganization.organization.refresh();
+            favoriteOrganization.organizationName = organization.name;
+            favoriteOrganization.update();
+        }
+    }
 }
app/models/FavoriteProject.java
--- app/models/FavoriteProject.java
+++ app/models/FavoriteProject.java
@@ -8,10 +8,12 @@
 
 import play.db.ebean.Model;
 
+import javax.annotation.Nonnull;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.ManyToOne;
 import javax.persistence.OneToOne;
+import java.util.List;
 
 @Entity
 public class FavoriteProject extends Model {
@@ -36,4 +38,15 @@
         this.owner = project.owner;
         this.projectName = project.name;
     }
+
+    public static void updateFavoriteProject(@Nonnull Project project){
+        List<FavoriteProject> favoriteProjects = finder.where().eq("project.id", project.id).findList();
+
+        for (FavoriteProject favoriteProject : favoriteProjects) {
+            favoriteProject.project.refresh();
+            favoriteProject.owner = project.owner;
+            favoriteProject.projectName = project.name;
+            favoriteProject.update();
+        }
+    }
 }
app/models/User.java
--- app/models/User.java
+++ app/models/User.java
@@ -880,6 +880,7 @@
     public List<Project> getFavoriteProjects() {
         List<Project> projects = new ArrayList<>();
         for (FavoriteProject favoriteProject : this.favoriteProjects) {
+            favoriteProject.project.refresh();
             projects.add(0, favoriteProject.project);
         }
 
@@ -932,6 +933,7 @@
     public List<Organization> getFavoriteOrganizations() {
         List<Organization> organizations = new ArrayList<>();
         for (FavoriteOrganization favoriteOrganization : this.favoriteOrganizations) {
+            favoriteOrganization.organization.refresh();
             organizations.add(0, favoriteOrganization.organization);
         }
 
Add a comment
List