Skip to content

Commit 4364fd1

Browse files
HimanshiVerma05himanshi
andauthored
Minor refactoring - Design level refactors (#2781)
* Minor refactoring - Replace conditional with polymorphism. * Minor refactoring - Move Field - worked on comment - // These 2 should be (public) constants on their respective service classes, not here * Minor refactoring - Rename Field - making the code more readable * Minor refactoring - Rename Field - making the code more readable * Minor refactoring - Move method - the method getUserLayoutTuple seems more relevant to IUserLayoutStore. Hence moving it * Minor refactoring - Extract class PortalaConstants . Identified a cohesive set of variables/constants and grouped them into their own class. * formatting * Refactoring - Extract class - Extracted event coordination helper methods from the PortletEventCoordinatationService into a new class named PortletEventCoordinationHelper. * Refactoring - Extract class - Extracted event coordination helper methods from the PortletEventCoordinatationService into a new class named PortletEventCoordinationHelper. --------- Co-authored-by: himanshi <[email protected]>
1 parent 6fad6aa commit 4364fd1

File tree

15 files changed

+447
-384
lines changed

15 files changed

+447
-384
lines changed

uPortal-api/uPortal-api-rest/src/main/java/org/apereo/portal/rest/PagsRESTController.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@ public class PagsRESTController {
7575
* This step is necessary; the incoming URLs will sometimes have '+'
7676
* characters for spaces, and the @PathVariable magic doesn't convert them.
7777
*/
78-
String name;
78+
String decodedPagsGroupName;
7979
try {
80-
name = URLDecoder.decode(pagsGroupName, "UTF-8");
80+
decodedPagsGroupName = URLDecoder.decode(pagsGroupName, "UTF-8");
8181
} catch (UnsupportedEncodingException e) {
8282
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
8383
return "{ 'error': '" + e.toString() + "' }";
8484
}
8585

8686
IPerson person = personManager.getPerson(request);
8787
IPersonAttributesGroupDefinition pagsGroup =
88-
this.pagsService.getPagsDefinitionByName(person, name);
88+
this.pagsService.getPagsDefinitionByName(person, decodedPagsGroupName);
8989
return respondPagsGroupJson(res, pagsGroup, person, HttpServletResponse.SC_FOUND);
9090
}
9191

@@ -106,9 +106,9 @@ public class PagsRESTController {
106106
* This step is necessary; the incoming URLs will sometimes have '+'
107107
* characters for spaces, and the @PathVariable magic doesn't convert them.
108108
*/
109-
String name;
109+
String decodedParentGroupName;
110110
try {
111-
name = URLDecoder.decode(parentGroupName, "UTF-8");
111+
decodedParentGroupName = URLDecoder.decode(parentGroupName, "UTF-8");
112112
} catch (UnsupportedEncodingException e) {
113113
res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
114114
return "{ 'error': '" + e.getMessage() + "' }";
@@ -125,10 +125,12 @@ public class PagsRESTController {
125125
// Obtain a real reference to the parent group
126126
EntityIdentifier[] eids =
127127
GroupService.searchForGroups(
128-
name, IGroupConstants.SearchMethod.DISCRETE, IPerson.class);
128+
decodedParentGroupName,
129+
IGroupConstants.SearchMethod.DISCRETE,
130+
IPerson.class);
129131
if (eids.length == 0) {
130132
res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
131-
return "{ 'error': 'Parent group does not exist: " + name + "' }";
133+
return "{ 'error': 'Parent group does not exist: " + decodedParentGroupName + "' }";
132134
}
133135
IEntityGroup parentGroup =
134136
(IEntityGroup) GroupService.getGroupMember(eids[0]); // Names must be unique

uPortal-groups/uPortal-groups-pags/src/main/java/org/apereo/portal/groups/pags/dao/EntityPersonAttributesGroupStore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apereo.portal.groups.pags.IPersonTester;
3939
import org.apereo.portal.groups.pags.PagsGroup;
4040
import org.apereo.portal.groups.pags.TestGroup;
41+
import org.apereo.portal.groups.pags.dao.jpa.PagsGroupService;
4142
import org.apereo.portal.security.IPerson;
4243
import org.apereo.portal.security.PersonFactory;
4344
import org.apereo.portal.security.provider.RestrictedPerson;
@@ -111,7 +112,7 @@ public boolean contains(IEntityGroup group, IGroupMember member) {
111112
if (member.isGroup()) {
112113
// PAGS groups may only contain other PAGS groups (and people, of course)
113114
final IEntityGroup ieg = (IEntityGroup) member;
114-
if (!PagsService.SERVICE_NAME_PAGS.equals(ieg.getServiceName().toString())) {
115+
if (!PagsGroupService.SERVICE_NAME_PAGS.equals(ieg.getServiceName().toString())) {
115116
return false;
116117
}
117118
}
@@ -233,7 +234,7 @@ public Iterator<IEntityGroup> findParentGroups(IGroupMember member) throws Group
233234
if (member.isGroup()) {
234235
// PAGS groups may only contain other PAGS groups (and people, of course)
235236
final IEntityGroup ieg = (IEntityGroup) member;
236-
if (PagsService.SERVICE_NAME_PAGS.equals(ieg.getServiceName().toString())) {
237+
if (PagsGroupService.SERVICE_NAME_PAGS.equals(ieg.getServiceName().toString())) {
237238
result = findParentGroupsForGroup((IEntityGroup) member);
238239
}
239240
} else {

uPortal-groups/uPortal-groups-pags/src/main/java/org/apereo/portal/groups/pags/dao/PagsService.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
*/
1515
package org.apereo.portal.groups.pags.dao;
1616

17+
import static org.apereo.portal.groups.pags.dao.jpa.LocalGroupService.SERVICE_NAME_LOCAL;
18+
import static org.apereo.portal.groups.pags.dao.jpa.PagsGroupService.SERVICE_NAME_PAGS;
19+
1720
import java.util.HashSet;
1821
import java.util.Set;
1922
import java.util.regex.Pattern;
2023
import org.apache.commons.lang3.StringUtils;
2124
import org.apereo.portal.EntityIdentifier;
2225
import org.apereo.portal.groups.IEntityGroup;
2326
import org.apereo.portal.groups.IGroupConstants;
27+
import org.apereo.portal.groups.pags.dao.jpa.LocalGroupService;
28+
import org.apereo.portal.groups.pags.dao.jpa.PagsGroupService;
2429
import org.apereo.portal.portlet.om.IPortletDefinition;
2530
import org.apereo.portal.security.IAuthorizationPrincipal;
2631
import org.apereo.portal.security.IPermission;
@@ -42,9 +47,8 @@
4247
@Service
4348
public final class PagsService {
4449

45-
// These 2 should be (public) constants on their respective service classes, not here
46-
private static final String SERVICE_NAME_LOCAL = "local";
47-
public static final String SERVICE_NAME_PAGS = "pags";
50+
@Autowired private PagsGroupService pagsGroupService;
51+
@Autowired private LocalGroupService localGroupService;
4852

4953
private static final String GROUP_NAME_VALIDATOR_REGEX = "^[\\w ]{5,500}$"; // 5-500 characters
5054
private static final Pattern GROUP_NAME_VALIDATOR_PATTERN =
@@ -152,29 +156,15 @@ public IPersonAttributesGroupDefinition createPagsDefinition(
152156
IPersonAttributesGroupDefinition result =
153157
pagsGroupDefDao.createPersonAttributesGroupDefinition(groupName, description);
154158
if (parent != null) {
155-
// Should refactor this switch to instead choose a service and invoke a method on it
159+
156160
switch (parent.getServiceName().toString()) {
157161
case SERVICE_NAME_LOCAL:
158-
IEntityGroup member =
159-
GroupService.findGroup(
160-
result.getCompositeEntityIdentifierForGroup().getKey());
161-
if (member == null) {
162-
String msg =
163-
"The specified group was created, but is not present in the store: "
164-
+ result.getName();
165-
throw new RuntimeException(msg);
166-
}
167-
parent.addChild(member);
168-
parent.updateMembers();
162+
localGroupService.addMember(parent, result);
169163
break;
170164
case SERVICE_NAME_PAGS:
171165
IPersonAttributesGroupDefinition parentDef =
172166
getPagsGroupDefByName(parent.getName());
173-
Set<IPersonAttributesGroupDefinition> members =
174-
new HashSet<>(parentDef.getMembers());
175-
members.add(result);
176-
parentDef.setMembers(members);
177-
pagsGroupDefDao.updatePersonAttributesGroupDefinition(parentDef);
167+
pagsGroupService.addMember(parentDef, result);
178168
break;
179169
default:
180170
String msg =
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.apereo.portal.groups.pags.dao.jpa;
2+
3+
import org.apereo.portal.groups.IEntityGroup;
4+
import org.apereo.portal.groups.pags.dao.IPersonAttributesGroupDefinition;
5+
import org.springframework.stereotype.Service;
6+
7+
@Service
8+
public class LocalGroupService {
9+
public static final String SERVICE_NAME_LOCAL = "local";
10+
11+
public void addMember(IEntityGroup parent, IPersonAttributesGroupDefinition result) {
12+
IEntityGroup member =
13+
org.apereo.portal.services.GroupService.findGroup(
14+
result.getCompositeEntityIdentifierForGroup().getKey());
15+
if (member == null) {
16+
String msg =
17+
"The specified group was created, but is not present in the store: "
18+
+ result.getName();
19+
throw new RuntimeException(msg);
20+
}
21+
parent.addChild(member);
22+
parent.updateMembers();
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.apereo.portal.groups.pags.dao.jpa;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import org.apereo.portal.groups.pags.dao.IPersonAttributesGroupDefinition;
6+
import org.apereo.portal.groups.pags.dao.IPersonAttributesGroupDefinitionDao;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
12+
@Service
13+
public class PagsGroupService {
14+
public static final String SERVICE_NAME_PAGS = "pags";
15+
@Autowired private IPersonAttributesGroupDefinitionDao pagsGroupDefDao;
16+
private final Logger logger = LoggerFactory.getLogger(getClass());
17+
18+
public void addMember(
19+
IPersonAttributesGroupDefinition parentDef, IPersonAttributesGroupDefinition result) {
20+
21+
Set<IPersonAttributesGroupDefinition> members = new HashSet<>(parentDef.getMembers());
22+
members.add(result);
23+
parentDef.setMembers(members);
24+
pagsGroupDefDao.updatePersonAttributesGroupDefinition(parentDef);
25+
}
26+
}

uPortal-groups/uPortal-groups-pags/src/main/java/org/apereo/portal/groups/pags/dao/jpa/PersonAttributesGroupDefinitionImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.apereo.portal.EntityIdentifier;
4242
import org.apereo.portal.groups.pags.dao.IPersonAttributesGroupDefinition;
4343
import org.apereo.portal.groups.pags.dao.IPersonAttributesGroupTestGroupDefinition;
44-
import org.apereo.portal.groups.pags.dao.PagsService;
4544
import org.apereo.portal.security.IPerson;
4645
import org.dom4j.DocumentHelper;
4746
import org.dom4j.QName;
@@ -135,7 +134,7 @@ public long getId() {
135134
@JsonIgnore
136135
public EntityIdentifier getCompositeEntityIdentifierForGroup() {
137136
return new EntityIdentifier(
138-
PagsService.SERVICE_NAME_PAGS + "." + this.getName(), IPerson.class);
137+
PagsGroupService.SERVICE_NAME_PAGS + "." + this.getName(), IPerson.class);
139138
}
140139

141140
@Override

uPortal-layout/uPortal-layout-core/src/main/java/org/apereo/portal/layout/IUserLayoutStore.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,19 @@ void setUserLayout(
199199
* with greater precedence come before those with lower precedence.
200200
*/
201201
double getFragmentPrecedence(long id);
202+
203+
/**
204+
* Provides a {@link Tuple} containing the &quot;fragmentized&quot; version of a DLM fragment
205+
* owner's layout, together with the username. This version of the layout consistent with what
206+
* DLM uses internally for fragments, and is created by FragmentActivator.fragmentizeLayout.
207+
* It's important that the version returned by this method matches what DLM uses internally
208+
* because it will be used to establish relationships between fragment layout nodes and user
209+
* customizations of DLM fragments.
210+
*
211+
* @param userName The username of the user for whom the layout is retrieved.
212+
* @param userId The unique identifier of the user.
213+
* @return A {@link Tuple} containing the username and the "fragmentized" version of the DLM
214+
* fragment owner's layout.
215+
*/
216+
Tuple<String, DistributedUserLayout> getUserLayoutTuple(String userName, int userId);
202217
}

uPortal-layout/uPortal-layout-impl/src/main/java/org/apereo/portal/layout/dlm/NodeReferenceFactory.java

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
2424
import org.apereo.portal.IUserIdentityStore;
25-
import org.apereo.portal.IUserProfile;
26-
import org.apereo.portal.UserProfile;
2725
import org.apereo.portal.layout.IUserLayoutStore;
28-
import org.apereo.portal.security.provider.BrokenSecurityContext;
29-
import org.apereo.portal.security.provider.PersonImpl;
3026
import org.apereo.portal.utils.Tuple;
3127
import org.apereo.portal.xml.XmlUtilities;
3228
import org.apereo.portal.xml.xpath.XPathOperations;
@@ -47,7 +43,7 @@ public final class NodeReferenceFactory {
4743
private static final Pattern DLM_PATH_REF_DELIM = Pattern.compile("\\:");
4844
private static final Pattern USER_NODE_PATTERN = Pattern.compile("\\A([a-zA-Z]\\d*)\\z");
4945
private static final Pattern DLM_NODE_PATTERN = Pattern.compile("u(\\d+)l\\d+([ns]\\d+)");
50-
46+
private static final int LAYOUT_ID_FIRST_NODE = 1;
5147
private final Log log = LogFactory.getLog(getClass());
5248

5349
@Autowired private IUserLayoutStore layoutStore;
@@ -142,7 +138,7 @@ public Noderef getNoderefFromPathref(
142138
}
143139

144140
final Tuple<String, DistributedUserLayout> userLayoutInfo =
145-
getUserLayoutTuple(layoutOwnerName, layoutOwnerUserId);
141+
layoutStore.getUserLayoutTuple(layoutOwnerName, layoutOwnerUserId);
146142
final Document userLayout = userLayoutInfo.second.getLayout();
147143

148144
final Node targetNode =
@@ -178,21 +174,14 @@ public Noderef getNoderefFromPathref(
178174
final String structId = structIdAttr.getTextContent();
179175

180176
if (isStructRef) {
181-
return new Noderef(
182-
layoutOwnerUserId,
183-
1 /* TODO: remove hard-coded layoutId=1 */,
184-
"s" + structId);
177+
return new Noderef(layoutOwnerUserId, LAYOUT_ID_FIRST_NODE, "s" + structId);
185178
}
186179

187-
return new Noderef(
188-
layoutOwnerUserId, 1 /* TODO: remove hard-coded layoutId=1 */, "n" + structId);
180+
return new Noderef(layoutOwnerUserId, LAYOUT_ID_FIRST_NODE, "n" + structId);
189181
}
190182

191183
final Node idAttr = attributes.getNamedItem("ID");
192-
return new Noderef(
193-
layoutOwnerUserId,
194-
1 /* TODO: remove hard-coded layoutId=1 */,
195-
idAttr.getTextContent());
184+
return new Noderef(layoutOwnerUserId, LAYOUT_ID_FIRST_NODE, idAttr.getTextContent());
196185
}
197186

198187
/**
@@ -229,7 +218,7 @@ public Pathref getPathrefFromNoderef(
229218

230219
final String userName = this.userIdentityStore.getPortalUserName(userId);
231220
final Tuple<String, DistributedUserLayout> userLayoutInfo =
232-
getUserLayoutTuple(userName, userId);
221+
layoutStore.getUserLayoutTuple(userName, userId);
233222

234223
if (userLayoutInfo.second == null) {
235224
this.log.warn(
@@ -291,35 +280,4 @@ public Pathref getPathrefFromNoderef(
291280

292281
return result;
293282
}
294-
295-
/*
296-
* Implementation.
297-
*/
298-
299-
/**
300-
* Provides a {@link Tuple} containing the &quot;fragmentized&quot; version of a DLM fragment
301-
* owner's layout, together with the username. This version of the layout consistent with what
302-
* DLM uses internally for fragments, and is created by FragmentActivator.fragmentizeLayout.
303-
* It's important that the version returned by this method matches what DLM uses internally
304-
* because it will be used to establish relationships between fragment layout nodes and user
305-
* customizations of DLM fragments.
306-
*
307-
* @param userId
308-
* @return
309-
*/
310-
/* TODO: make private */ Tuple<String, DistributedUserLayout> getUserLayoutTuple(
311-
String userName, int userId) {
312-
313-
final PersonImpl person = new PersonImpl();
314-
person.setUserName(userName);
315-
person.setID(userId);
316-
person.setSecurityContext(new BrokenSecurityContext());
317-
318-
final IUserProfile profile =
319-
layoutStore.getUserProfileByFname(person, UserProfile.DEFAULT_PROFILE_FNAME);
320-
final DistributedUserLayout userLayout =
321-
layoutStore.getUserLayout(person, (UserProfile) profile);
322-
323-
return new Tuple<String, DistributedUserLayout>(userName, userLayout);
324-
}
325283
}

uPortal-layout/uPortal-layout-impl/src/main/java/org/apereo/portal/layout/dlm/RDBMDistributedLayoutStore.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333
import java.util.regex.Pattern;
3434
import net.sf.ehcache.Ehcache;
3535
import org.apache.commons.lang.StringUtils;
36-
import org.apereo.portal.AuthorizationException;
37-
import org.apereo.portal.IUserIdentityStore;
38-
import org.apereo.portal.IUserProfile;
39-
import org.apereo.portal.PortalException;
36+
import org.apereo.portal.*;
4037
import org.apereo.portal.io.xml.IPortalDataHandlerService;
4138
import org.apereo.portal.jdbc.RDBMServices;
4239
import org.apereo.portal.layout.LayoutStructure;
@@ -1873,6 +1870,33 @@ private interface FormOfLayoutCorruption {
18731870
String getMessage();
18741871
}
18751872

1873+
/**
1874+
* Provides a {@link Tuple} containing the &quot;fragmentized&quot; version of a DLM fragment
1875+
* owner's layout, together with the username. This version of the layout consistent with what
1876+
* DLM uses internally for fragments, and is created by FragmentActivator.fragmentizeLayout.
1877+
* It's important that the version returned by this method matches what DLM uses internally
1878+
* because it will be used to establish relationships between fragment layout nodes and user
1879+
* customizations of DLM fragments.
1880+
*
1881+
* @param userName The username of the user for whom the layout is retrieved.
1882+
* @param userId The unique identifier of the user.
1883+
* @return A {@link Tuple} containing the username and the "fragmentized" version of the DLM
1884+
* fragment owner's layout.
1885+
*/
1886+
@Override
1887+
public Tuple<String, DistributedUserLayout> getUserLayoutTuple(String userName, int userId) {
1888+
final PersonImpl person = new PersonImpl();
1889+
person.setUserName(userName);
1890+
person.setID(userId);
1891+
person.setSecurityContext(new BrokenSecurityContext());
1892+
1893+
final IUserProfile profile =
1894+
this.getUserProfileByFname(person, UserProfile.DEFAULT_PROFILE_FNAME);
1895+
final DistributedUserLayout userLayout = this.getUserLayout(person, profile);
1896+
1897+
return new Tuple<>(userName, userLayout);
1898+
}
1899+
18761900
private static final List<FormOfLayoutCorruption> KNOWN_FORMS_OF_LAYOUT_CORRUPTION =
18771901
Collections.unmodifiableList(
18781902
Arrays.asList(

0 commit comments

Comments
 (0)