39
39
import org .apereo .portal .layout .IUserLayoutManager ;
40
40
import org .apereo .portal .layout .IUserLayoutStore ;
41
41
import org .apereo .portal .layout .PortletSubscribeIdResolver ;
42
+ import org .apereo .portal .layout .dlm .MissingPortletDefinition ;
42
43
import org .apereo .portal .layout .dlm .UserPrefsHandler ;
43
44
import org .apereo .portal .layout .node .IUserLayoutChannelDescription ;
44
45
import org .apereo .portal .layout .node .IUserLayoutFolderDescription ;
51
52
import org .apereo .portal .portlet .registry .IPortletWindowRegistry ;
52
53
import org .apereo .portal .portlets .favorites .FavoritesUtils ;
53
54
import org .apereo .portal .security .IAuthorizationPrincipal ;
55
+ import org .apereo .portal .security .IAuthorizationService ;
54
56
import org .apereo .portal .security .IPermission ;
55
57
import org .apereo .portal .security .IPerson ;
56
58
import org .apereo .portal .security .PermissionHelper ;
57
- import org .apereo .portal .services .AuthorizationServiceFacade ;
58
59
import org .apereo .portal .services .GroupService ;
59
60
import org .apereo .portal .user .IUserInstance ;
60
61
import org .apereo .portal .user .IUserInstanceManager ;
62
+ import org .apereo .portal .utils .personalize .IPersonalizer ;
61
63
import org .slf4j .Logger ;
62
64
import org .slf4j .LoggerFactory ;
63
65
import org .springframework .beans .factory .annotation .Autowired ;
@@ -86,6 +88,7 @@ public class UpdatePreferencesServlet {
86
88
87
89
protected final Logger logger = LoggerFactory .getLogger (getClass ());
88
90
91
+ private IAuthorizationService authorizationService ;
89
92
private IPortletDefinitionRegistry portletDefinitionRegistry ;
90
93
private IUserIdentityStore userIdentityStore ;
91
94
private IUserInstanceManager userInstanceManager ;
@@ -94,6 +97,7 @@ public class UpdatePreferencesServlet {
94
97
private MessageSource messageSource ;
95
98
private IPortletWindowRegistry portletWindowRegistry ;
96
99
private FavoritesUtils favoritesUtils ;
100
+ private IPersonalizer personalizer ;
97
101
98
102
@ Value ("${org.apereo.portal.layout.dlm.remoting.addedWindowState:null}" )
99
103
private String addedPortletWindowState ;
@@ -109,6 +113,11 @@ private void initAddedPortletWindowState() {
109
113
}
110
114
}
111
115
116
+ @ Autowired
117
+ public void setAuthorizationService (IAuthorizationService authorizationService ) {
118
+ this .authorizationService = authorizationService ;
119
+ }
120
+
112
121
@ Autowired
113
122
public void setUserLayoutStore (IUserLayoutStore userLayoutStore ) {
114
123
this .userLayoutStore = userLayoutStore ;
@@ -150,6 +159,11 @@ public void setFavoritesUtils(FavoritesUtils favoritesUtils) {
150
159
this .favoritesUtils = favoritesUtils ;
151
160
}
152
161
162
+ @ Autowired
163
+ public void setPersonalizer (IPersonalizer personalizer ) {
164
+ this .personalizer = personalizer ;
165
+ }
166
+
153
167
/** Default name given to newly created tabs. */
154
168
protected static final String DEFAULT_TAB_NAME = "New Tab" ; // TODO: Requires i18n!
155
169
@@ -606,13 +620,15 @@ public ModelAndView addFavorite(
606
620
final IUserLayoutManager ulm = upm .getUserLayoutManager ();
607
621
608
622
final IUserLayoutChannelDescription channel =
609
- new UserLayoutChannelDescription (person , pdef , request .getSession ());
623
+ new UserLayoutChannelDescription (person , pdef , request .getSession (), personalizer );
610
624
611
625
// get favorite tab
612
626
final String favoriteTabNodeId = favoritesUtils .getFavoriteTabNodeId (ulm .getUserLayout ());
613
627
614
628
if (favoriteTabNodeId != null ) {
615
- // add portlet to favorite tab
629
+ removeOrphanedFavoritesFromLayout (ulm );
630
+
631
+ // add new favorite portlet to favorite tab
616
632
final IUserLayoutNodeDescription node = addNodeToTab (ulm , channel , favoriteTabNodeId );
617
633
618
634
if (node == null ) {
@@ -696,12 +712,13 @@ public ModelAndView removeFavorite(
696
712
697
713
String resp = new String ();
698
714
715
+ removeOrphanedFavoritesFromLayout (ulm , favoritePortlets );
716
+
717
+ boolean saveNeeded = false ;
699
718
for (IUserLayoutChannelDescription deleteNode : favoritesToDelete ) {
700
- if (deleteNode != null && deleteNode instanceof UserLayoutChannelDescription ) {
701
- UserLayoutChannelDescription channelDescription =
702
- (UserLayoutChannelDescription ) deleteNode ;
719
+ if (deleteNode != null ) {
703
720
try {
704
- if (!ulm .deleteNode (channelDescription .getChannelSubscribeId ())) {
721
+ if (!ulm .deleteNode (deleteNode .getChannelSubscribeId ())) {
705
722
706
723
logger .warn (
707
724
"Error deleting the node {} from favorites for user {}" ,
@@ -718,27 +735,30 @@ public ModelAndView removeFavorite(
718
735
"Can''t remove favorite" ,
719
736
locale );
720
737
} else {
738
+ saveNeeded = true ;
721
739
// load success message
722
-
723
- resp =
724
- getMessage (
725
- "success.remove.portlet" ,
726
- "Removed from Favorites successfully" ,
727
- locale );
728
740
}
729
- // save the user's layout
741
+ } catch (PortalException e ) {
742
+ return handlePersistError (request , response , e );
743
+ }
744
+ }
745
+ if (saveNeeded ) {
746
+ try {
730
747
ulm .saveUserLayout ();
731
748
} catch (PortalException e ) {
732
749
return handlePersistError (request , response , e );
733
750
}
751
+ resp =
752
+ getMessage (
753
+ "success.remove.portlet" ,
754
+ "Removed from Favorites successfully" ,
755
+ locale );
734
756
}
735
757
}
736
758
737
759
return new ModelAndView ("jsonView" , Collections .singletonMap ("response" , resp ));
738
760
}
739
761
740
- // save the user's layout
741
- ulm .saveUserLayout ();
742
762
return new ModelAndView (
743
763
"jsonView" ,
744
764
Collections .singletonMap (
@@ -789,7 +809,7 @@ else if (fname != null)
789
809
790
810
IUserLayoutChannelDescription channel =
791
811
new UserLayoutChannelDescription (
792
- getPerson (ui , response ), definition , request .getSession ());
812
+ getPerson (ui , response ), definition , request .getSession (), personalizer );
793
813
794
814
IUserLayoutNodeDescription node ;
795
815
if (isTab (ulm , destinationId )) {
@@ -1453,13 +1473,12 @@ protected String getMessage(String key, String argument, String defaultMessage,
1453
1473
}
1454
1474
1455
1475
private IAuthorizationPrincipal getUserPrincipal (final String userName ) {
1456
- final IEntity user = GroupService . getEntity (userName , IPerson . class );
1476
+ final IEntity user = this . personEntityService . getPersonEntity (userName );
1457
1477
if (user == null ) {
1458
1478
return null ;
1459
1479
}
1460
1480
1461
- final AuthorizationServiceFacade authService = AuthorizationServiceFacade .instance ();
1462
- return authService .newPrincipal (user );
1481
+ return authorizationService .newPrincipal (user );
1463
1482
}
1464
1483
1465
1484
private String getTabIdFromName (IUserLayout userLayout , String tabName ) {
@@ -1595,4 +1614,49 @@ private boolean attemptNodeMove(
1595
1614
private boolean isFolder (IUserLayoutManager ulm , String id ) {
1596
1615
return ulm .getNode (id ).getType ().equals (IUserLayoutNodeDescription .LayoutNodeType .FOLDER );
1597
1616
}
1617
+
1618
+ private void removeOrphanedFavoritesFromLayout (IUserLayoutManager ulm ) {
1619
+ List <IUserLayoutNodeDescription > favoritesPortlets =
1620
+ favoritesUtils .getFavoritePortletLayoutNodes (ulm .getUserLayout ());
1621
+ removeOrphanedFavoritesFromLayout (ulm , favoritesPortlets );
1622
+ }
1623
+
1624
+ private void removeOrphanedFavoritesFromLayout (
1625
+ IUserLayoutManager ulm , List <IUserLayoutNodeDescription > favoritesPortlets ) {
1626
+ // remove orphaned favorites (meaning they are favorites for portlets that have been
1627
+ // removed/deleted) ...keep in mind that ulm.saveLayout() will need to be called
1628
+ // in order to actually save the changes to the layout
1629
+ for (IUserLayoutNodeDescription node : favoritesPortlets ) {
1630
+ if (node instanceof IUserLayoutChannelDescription ) {
1631
+ IUserLayoutChannelDescription channelDescription =
1632
+ (IUserLayoutChannelDescription ) node ;
1633
+ if (channelDescription
1634
+ .getChannelPublishId ()
1635
+ .equals (MissingPortletDefinition .CHANNEL_ID )) {
1636
+ ulm .deleteNode (channelDescription .getChannelSubscribeId ());
1637
+ }
1638
+ }
1639
+ }
1640
+ }
1641
+
1642
+ // for unit testing begin
1643
+
1644
+ public interface IPersonEntityService {
1645
+ IEntity getPersonEntity (String userName );
1646
+ }
1647
+
1648
+ public class GroupServiceBasedPersonEntityService implements IPersonEntityService {
1649
+ @ Override
1650
+ public IEntity getPersonEntity (String userName ) {
1651
+ return GroupService .getEntity (userName , IPerson .class );
1652
+ }
1653
+ }
1654
+
1655
+ private IPersonEntityService personEntityService = new GroupServiceBasedPersonEntityService ();
1656
+
1657
+ public void setPersonEntityService (IPersonEntityService personEntityService ) {
1658
+ this .personEntityService = personEntityService ;
1659
+ }
1660
+
1661
+ // for unit testing end
1598
1662
}
0 commit comments