📄 personalizationservice.java
字号:
package com.esri.solutions.jitk.services.personalization;
import java.security.Principal;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServletEndpointContext;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import com.esri.solutions.jitk.services.common.ServicesException;
/**
* {@code PersonalizationService} is an abstract base class that provides functionality
* common across all Personalization Web Services.
* <p>
* This functionality includes:
* <ul>
* <li>
* Initializing parameters that are used by Personalization Web Services, including:
* <ul>
* <li>Checking for duplicate IDs on Insert (see checkForDuplicateIdOnInsert in web.xml)</li>
* </ul>
* </li>
* <li>Determining if a given ID exists in the datastore</li>
* <li>Retrieving the name of the logged-in user</li>
* </ul>
*/
public abstract class PersonalizationService extends ServletEndpointSupport {
protected boolean checkForDuplicateIds = false;
protected static final String CONTEXT_PARAM_CHECK_FOR_DUPLICATE_IDS = "checkForDuplicateIdOnInsert";
protected static final String ERROR_ID_ALREADY_EXISTS = "A {0} with an ID of {1} already exists.";
protected static final String ERROR_CANNOT_DETERMINE_LOGGED_IN_USER = "Cannot determine logged in user.";
public PersonalizationService() {
super();
}
/**
* {@inheritDoc}
* <p>
* This version of {@code onInit()} retrieves the value of the {@code checkForDuplicateIdOnInsert} parameter from the application's
* deployment descriptor and stores it locally.
* </p>
*/
protected void onInit() throws ServiceException {
super.onInit();
Object o = getServletContext().getInitParameter(
CONTEXT_PARAM_CHECK_FOR_DUPLICATE_IDS);
checkForDuplicateIds = Boolean.valueOf((String) o);
}
/**
* Given the ID of an item that is manipulated by the Personalization
* Service, this method will determine if an item with that ID already
* exists in the database
*
* @param id
* the id of the item to insert
* @return {@code boolean} {@code true} if the item exists; {@code false}
* otherwise
* @throws ServicesException
* If an exception occurs when checking the data in the database
*/
protected boolean okToInsert(String id) throws ServicesException {
boolean okToInsert = true;
if (checkForDuplicateIds) {
okToInsert = !checkForId(id);
}
return okToInsert;
}
/**
* Abstract method that checks, in a service-specific manner, the datastore
* to see if an ID already exists.
*
* @param id
* {@link String} containing the ID whose existence shoudl be
* determined
* @return {@code boolean} {@code true} if the id exists; {@code false}
* otherwise
* @throws ServicesException
* If an exception occurs when checking the data in the database
*/
protected abstract boolean checkForId(String id) throws ServicesException;
/**
* Retrieves the name of the currently-logged-in user.
* <p>
* This is a convenience method that exists as a shortcut for the call:
* {@link #getServletEndpointContext()#getUserPrincipal()#getName()}.
* </p>
*
* @return
* @throws ServicesException
* If an exception occurs when trying to deterime the username.
*/
protected String getUsername() throws ServicesException {
String name;
try {
ServletEndpointContext context = getServletEndpointContext();
Principal principal = context.getUserPrincipal();
name = principal.getName();
} catch (Exception e) {
throw new ServicesException(ERROR_CANNOT_DETERMINE_LOGGED_IN_USER,
e);
}
return name;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -