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

Issue 13192 edit mode cleanup #13193

Merged
merged 49 commits into from
Dec 27, 2017
Merged

Issue 13192 edit mode cleanup #13193

merged 49 commits into from
Dec 27, 2017

Conversation

wezell
Copy link
Contributor

@wezell wezell commented Dec 8, 2017

This replaced the nasty code we have everywhere

boolean live = true;
boolean ADMIN_MODE = (session.getAttribute(com.dotmarketing.util.WebKeys.ADMIN_MODE_SESSION) != null);
boolean PREVIEW_MODE = ((session.getAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION) != null) && ADMIN_MODE);
boolean EDIT_MODE = ((session.getAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION) != null) && ADMIN_MODE);
if (EDIT_MODE || PREVIEW_MODE) {
	live = false;
}

with

PageMode mode = PageMode.get(request);
PageMode mode = PageMode.get(session);

mode.isAdmin ; 
mode.showLive ; 
mode.respectAnonPerms ;

if( PageMode.get(request).isAdmin){ doAdminStuff(); }

contentAPI.find("sad", user, mode.respectAnonPerms);

contentAPI.findByIdentifier("id", 1, mode.showLive, user, mode.respectAnonPerms);

PageMode.setPageMode(request, PageMode.PREVIEW_MODE);

etc....

@@ -258,9 +258,14 @@
public static final String ADMIN_MODE_COOKIE = "ADMIN_MODE_COOKIE";

// SESSION ATTRIBUTES
/*
public static final String EDIT_MODE_SESSION = "com.dotmarketing.EDIT_MODE_SESSION";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR This block of commented-out lines of code should be removed. rule

@@ -41,8 +42,7 @@ public void init(Object obj) {

if (session != null) {
timemachine = session.getAttribute("tm_date") != null;
ADMIN_MODE = !timemachine && session != null
&& (session.getAttribute(com.dotmarketing.util.WebKeys.ADMIN_MODE_SESSION) != null);
boolean ADMIN_MODE = PageMode.get(request) .isAdmin;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Rename "ADMIN_MODE" which hides the field declared at line 33. rule
MAJOR Remove this useless assignment to local variable "ADMIN_MODE". rule
MINOR Remove this unused "ADMIN_MODE" local variable. rule
MINOR Rename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'. rule


public static PageMode get(HttpSession ses) {

PageMode mode = (ses != null && ses.getAttribute(com.dotmarketing.util.WebKeys.PAGE_MODE_SESSION) != null

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Immediately return this expression instead of assigning it to the temporary variable "mode". rule






Optional<ShortyId> shortOpt = APILocator.getShortyAPI().getShorty(id);
User user = WebAPILocator.getUserWebAPI().getLoggedInFrontendUser(request);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this useless assignment to local variable "user". rule
MINOR Remove this unused "user" local variable. rule


PageMode mode = (EDIT_MODE)
? PageMode.EDIT
: (PREVIEW_MODE)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Extract this nested ternary operation into an independent statement. rule

? PageMode.EDIT
: (PREVIEW_MODE)
? PageMode.PREVIEW
: (ADMIN_MODE)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Extract this nested ternary operation into an independent statement. rule

@@ -117,21 +113,15 @@ public Response searchRaw(@Context HttpServletRequest request) {

HttpSession session = request.getSession();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this useless assignment to local variable "session". rule
MINOR Remove this unused "session" local variable. rule

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove this unused import 'javax.servlet.http.HttpSession'. rule



PageMode mode = PageMode.get(req);
EDIT_OR_PREVIEW_MODE=!mode.showLive;
if(session!=null){
tmDate = (String) session.getAttribute("tm_date");
boolean tm=tmDate!=null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this useless assignment to local variable "tm". rule
MINOR Remove this unused "tm" local variable. rule

final Class classToUse) throws ParseException, IllegalAccessException,
InvocationTargetException, InstantiationException, NoSuchMethodException, NoSuchFieldException {
public static <T> List<T> convertDotConnectMapToPOJO(List<Map<String, Object>> results, final Class classToUse)
throws IllegalAccessException, InstantiationException,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove the declaration of thrown exception 'java.lang.IllegalAccessException', as it cannot be thrown from method's body. rule
MINOR Remove the declaration of thrown exception 'java.lang.InstantiationException', as it cannot be thrown from method's body. rule

InvocationTargetException, InstantiationException, NoSuchMethodException, NoSuchFieldException {
public static <T> List<T> convertDotConnectMapToPOJO(List<Map<String, Object>> results, final Class classToUse)
throws IllegalAccessException, InstantiationException,
NoSuchMethodException, NoSuchFieldException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove the declaration of thrown exception 'java.lang.NoSuchMethodException', as it cannot be thrown from method's body. rule
MINOR Remove the declaration of thrown exception 'java.lang.NoSuchFieldException', as it cannot be thrown from method's body. rule

hostVariablesTemplate.merge(context, out);
template.merge(context, out);

} catch (ParseErrorException e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Either log or rethrow this exception. rule

@@ -30,8 +30,6 @@ public String getName() {
return "fixBreaks";
}



final public boolean render(InternalContextAdapter context, Writer writer, Node node)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Reorder the modifiers to comply with the Java Language Specification. rule

@@ -30,8 +30,6 @@ public String getName() {
return "fixBreaks";
}



final public boolean render(InternalContextAdapter context, Writer writer, Node node)
throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove the declaration of thrown exception 'org.apache.velocity.exception.ResourceNotFoundException' which is a runtime exception. rule
MINOR Remove the declaration of thrown exception 'org.apache.velocity.exception.ParseErrorException' which is a runtime exception. rule
MINOR Remove the declaration of thrown exception 'org.apache.velocity.exception.MethodInvocationException' which is a runtime exception. rule


try {
backendUser = com.liferay.portal.util.PortalUtil.getUser(request);
// Skin skin = backendUser.getSkin();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR This block of commented-out lines of code should be removed. rule

* @throws DotDataException
* @throws DotSecurityException
*/
public static IHTMLPage getPage(Identifier id, HttpServletRequest request, boolean live, Context context) throws DotDataException, DotSecurityException{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Split this 153 characters long line (which is greater than 150 authorized). rule

context.put("dotPageMode", mode);
Date moddate = htmlPage.getModDate();

moddate = new Date(moddate.getTime());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Use the Java 8 Date and Time API instead. rule


//get the context from the request if possible
ChainedContext context;
if ( request.getAttribute( com.dotcms.rendering.velocity.Constants.VELOCITY_CONTEXT ) != null && request.getAttribute( com.dotcms.rendering.velocity.Constants.VELOCITY_CONTEXT ) instanceof ChainedContext ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Split this 215 characters long line (which is greater than 150 authorized). rule

* @throws DotSecurityException
* @throws DotDataException
*/
public static String getPageCacheKey(HttpServletRequest request, HttpServletResponse response) throws DotDataException, DotSecurityException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Reduce the number of returns of this method 6, down to the maximum allowed 3. rule
MAJOR Remove this unused method parameter "response". rule

return (ChainedContext) request.getAttribute( "velocityContext" );
} else {
RequestWrapper rw = new RequestWrapper( request );
if ( request.getAttribute( "User-Agent" ) != null && request.getAttribute( "User-Agent" ).equals( Constants.USER_AGENT_DOTCMS_BROWSER ) ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Split this 151 characters long line (which is greater than 150 authorized). rule

+ " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse() + "and language_id = "
+ (String) request.getSession().getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE) + ")", "UTF-8");
context.put("view", view);
} catch (Exception e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Catch a list of specific exception subtypes instead. rule

public class VelocityUtil {

private static VelocityEngine ve = null;
private static boolean DEFAULT_PAGE_TO_DEFAULT_LANGUAGE = LanguageWebAPI.canDefaultPageToDefaultLanguage();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Rename this field "DEFAULT_PAGE_TO_DEFAULT_LANGUAGE" to match the regular expression '^[a-z][a-zA-Z0-9]*$'. rule

private static VelocityEngine ve = null;
private static boolean DEFAULT_PAGE_TO_DEFAULT_LANGUAGE = LanguageWebAPI.canDefaultPageToDefaultLanguage();

private synchronized static void init(){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Reorder the modifiers to comply with the Java Language Specification. rule

private static boolean DEFAULT_PAGE_TO_DEFAULT_LANGUAGE = LanguageWebAPI.canDefaultPageToDefaultLanguage();

private synchronized static void init(){
if(ve != null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Missing curly brace. rule

}
// nocache passed either as a session var, as a request var or as a
// request attribute
if ("no".equals(request.getParameter("dotcache"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL Define a constant instead of duplicating this literal "dotcache" 3 times. rule

ve.init(SystemProperties.getProperties());

Logger.debug(VelocityUtil.class, SystemProperties.getProperties().toString());
}catch (Exception e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Catch a list of specific exception subtypes instead. rule

* if we have a toolbox manager, get a toolbox from it See
* /WEB-INF/toolbox.xml
*/
context.setToolbox(getToolboxManager().getToolboxContext(context));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove this use of "setToolbox"; it is deprecated. rule
MINOR Remove this use of "getToolboxContext"; it is deprecated. rule

// put the list of languages on the page
context.put("languages", getLanguages());
HttpSession session = request.getSession(false);
if(!UtilMethods.isSet(request.getAttribute(WebKeys.HTMLPAGE_LANGUAGE)) && session!=null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Missing curly brace. rule

HttpSession session = request.getSession(false);
if(!UtilMethods.isSet(request.getAttribute(WebKeys.HTMLPAGE_LANGUAGE)) && session!=null)
context.put("language", (String) session.getAttribute(com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE));
else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Missing curly brace. rule

backendUser = com.liferay.portal.util.PortalUtil.getUser(request);
// Skin skin = backendUser.getSkin();
// context.put("USER_SKIN", skin.getSkinId());
} catch (Exception nsue) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Catch a list of specific exception subtypes instead. rule

velocityRootPath = Config.getStringProperty("VELOCITY_ROOT", "/WEB-INF/velocity");
if (velocityRootPath.startsWith("/WEB-INF")) {
Logger.debug(VelocityUtil.class, "Velocity ROOT Path not found, defaulting it to '/WEB-INF/velocity'");
String startPath = velocityRootPath.substring(0, 8);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Assign this magic number 8 to a well-named constant, and use the constant instead. rule

Host host;
host = WebAPILocator.getHostWebAPI().getCurrentHost(request);
context.put("host", host);
} catch (Exception e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Catch a list of specific exception subtypes instead. rule

if (velocityRootPath.startsWith("/WEB-INF")) {
Logger.debug(VelocityUtil.class, "Velocity ROOT Path not found, defaulting it to '/WEB-INF/velocity'");
String startPath = velocityRootPath.substring(0, 8);
String endPath = velocityRootPath.substring(9, velocityRootPath.length());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Assign this magic number 9 to a well-named constant, and use the constant instead. rule


// to check user has permission to publish this page
boolean permission = APILocator.getPermissionAPI().doesUserHavePermission(htmlPage, PERMISSION_PUBLISH, backendUser);
context.put("permission", new Boolean(permission));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this "Boolean" constructor rule



String adminRoleKey = "";
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Extract this nested try block into a separate method. rule

Visitor visitor = (Visitor) request.getSession().getAttribute(WebKeys.VISITOR);
context.put("visitor", visitor);

} catch (Exception nsue) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Catch a list of specific exception subtypes instead. rule

try {
Role adminRole = APILocator.getRoleAPI().loadRoleByKey(Config.getStringProperty("CMS_ADMINISTRATOR_ROLE", "CMS Administrator"));
adminRoleKey = adminRole.getRoleKey();
} catch (Exception e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Either log or rethrow this exception. rule
MAJOR Either remove or fill this block of code. rule
MINOR Catch a list of specific exception subtypes instead. rule



public String parseVelocity(String velocityCode, Context ctx){
VelocityEngine ve = VelocityUtil.getEngine();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Rename "ve" which hides the field declared at line 60. rule

@jgambarios jgambarios merged commit 76d9671 into master Dec 27, 2017
@jgambarios jgambarios deleted the issue-13192-edit-mode-cleanup branch December 27, 2017 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants