@Override
//public Class<BatchCommon> getCommonPartClass() {
public Class getCommonPartClass() {
- try {
- return Class.forName("org.collectionspace.services.batch.BatchCommon");//.class;
- } catch (ClassNotFoundException e){
- return null;
- }
+ return BatchCommon.class;
}
- // FIXME: Resource classes should not be invoking handlers directly. This resource method should follow the conventions used by
++ /**
+ // other resource methods and use the getRepositoryClient() methods.
+ @Override
+ protected AbstractCommonList getCommonList(UriInfo ui) {
+ try {
+ ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
+ MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
+ DocumentHandler handler = createDocumentHandler(ctx);
+ String docType = queryParams.getFirst(IQueryManager.SEARCH_TYPE_DOCTYPE);
+ String mode = queryParams.getFirst(IQueryManager.SEARCH_TYPE_INVOCATION_MODE);
+ String whereClause = null;
+ DocumentFilter documentFilter = null;
+ String common_part = ctx.getCommonPartLabel();
+
+ if (docType != null && !docType.isEmpty()) {
+ whereClause = QueryManager.createWhereClauseForInvocableByDocType(
+ common_part, docType);
+ documentFilter = handler.getDocumentFilter();
+ documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
+ }
+
+ if (mode != null && !mode.isEmpty()) {
+ whereClause = QueryManager.createWhereClauseForInvocableByMode(
+ common_part, mode);
+ documentFilter = handler.getDocumentFilter();
+ documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
+ }
+
+ if (whereClause !=null && logger.isDebugEnabled()) {
+ logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
+ }
+
+ getRepositoryClient(ctx).getFiltered(ctx, handler);
+ AbstractCommonList list = (AbstractCommonList) handler.getCommonPartList();
+ return list;
+ } catch (Exception e) {
+ throw bigReThrow(e, ServiceMessages.LIST_FAILED);
+ }
+ }
+
/**
* Gets the authorityItem list for the specified authority
* If partialPerm is specified, keywords will be ignored.
private final Logger logger = LoggerFactory.getLogger(BatchDocumentModelHandler.class);
protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
-
- public InvocationResults invokeBatchJob(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String csid,
- ResourceMap<PoxPayloadIn, PoxPayloadOut> resourceMap, InvocationContext invocationCtx) throws Exception {
-
- String invocationMode = invocationCtx.getMode();
- String modeProperty = null;
- boolean checkDocType = true;
+
+ /**
+ * Return true if the batch job supports the requested mode.
+ * @param invocationCtx
+ * @param batchCommon
+ * @return
+ * @throws BadRequestException
+ */
+ protected boolean supportsInvokationMode(InvocationContext invocationCtx, BatchCommon batchCommon) throws BadRequestException {
+ boolean result = false;
+
+ String invocationMode = invocationCtx.getMode().toLowerCase();
if (BatchInvocable.INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationMode)) {
- modeProperty = BatchJAXBSchema.SUPPORTS_SINGLE_DOC;
+ result = batchCommon.isSupportsSingleDoc(); //BatchJAXBSchema.SUPPORTS_SINGLE_DOC;
} else if (BatchInvocable.INVOCATION_MODE_LIST.equalsIgnoreCase(invocationMode)) {
- modeProperty = BatchJAXBSchema.SUPPORTS_DOC_LIST;
+ result = batchCommon.isSupportsDocList(); //BatchJAXBSchema.SUPPORTS_DOC_LIST;
} else if (BatchInvocable.INVOCATION_MODE_GROUP.equalsIgnoreCase(invocationMode)) {
- modeProperty = BatchJAXBSchema.SUPPORTS_GROUP;
+ result = batchCommon.isSupportsGroup(); //BatchJAXBSchema.SUPPORTS_GROUP;
} else if (Invocable.INVOCATION_MODE_NO_CONTEXT.equalsIgnoreCase(invocationMode)) {
- modeProperty = InvocableJAXBSchema.SUPPORTS_NO_CONTEXT;
- checkDocType = false;
+ result = batchCommon.isSupportsNoContext(); //InvocableJAXBSchema.SUPPORTS_NO_CONTEXT;
} else {
- throw new BadRequestException("BatchResource: unknown Invocation Mode: " + invocationMode);
+ String msg = String.format("BatchResource: Unknown invocation mode '%s' requested trying to invoke batch job '%s'.",
+ invocationMode, batchCommon.getName());
+ throw new BadRequestException(msg);
+ }
+
+ return result;
+ }
+
+ /**
+ * Returns true if we found any required permissions.
+ *
+ * @param batchCommon
+ * @return
+ */
+ private boolean hasRequiredPermissions(BatchCommon batchCommon) {
+ boolean result = false;
+
+ try {
+ result = batchCommon.getResourceActionGroupList().getResourceActionGroup().size() > 0;
+ } catch (NullPointerException e) {
+ // ignore exception, we're just testing to see if we have any list elements
+ }
+
+ return result;
+ }
+
+ /**
+ * Returns true if we found any required roles.
+ *
+ * @param batchCommon
+ * @return
+ */
+ private boolean hasRequiredRoles(BatchCommon batchCommon) {
+ boolean result = false;
+
+ try {
+ result = batchCommon.getForRoles().getRoleDisplayName().size() > 0;
+ } catch (NullPointerException e) {
+ // ignore exception, we're just testing to see if we have any list elements
+ }
+
+ return result;
+ }
+
+ /**
+ * The current user is authorized to run the batch job if:
+ * 1. No permissions or roles are specified in the batch job
+ * 2. No roles are specified, but permissions are specified and the current user has those permissions
+ * 3. Roles are specified and the current user is a member of at least one of the roles.
+ *
+ * @param batchCommon
+ * @return
+ */
+ protected boolean isAuthoritzed(BatchCommon batchCommon) {
+ boolean result = true;
+
+ if (hasRequiredRoles(batchCommon)) {
+ result = isAuthorizedWithRoles(batchCommon);
+ } else if (hasRequiredPermissions(batchCommon)) {
+ result = isAuthoritzedWithPermissions(batchCommon);
+ }
+
+ return result;
+ }
+
+ protected boolean isAuthorizedWithRoles(BatchCommon batchCommon) {
+ boolean result = false;
+
+ ForRoles forRolesList = batchCommon.getForRoles();
+ if (forRolesList != null) {
+ AccountResource accountResource = new AccountResource();
+ List<String> roleDisplayNameList = accountResource.getAccountRoles(AuthN.get().getUserId(), AuthN.get().getCurrentTenantId());
+ for (String target : forRolesList.getRoleDisplayName()) {
+ if (roleDisplayNameList.contains(target)) {
+ result = true;
+ break;
+ }
+ }
}
+
+ return result;
+ }
+
+ /**
+ * Check to see if the current user is authorized to run/invoke this batch job. If the batch job
+ * did not specify any permissions, we assume that the current user is authorized to run the job.
+ * @param batchCommon
+ * @return
+ */
+ protected boolean isAuthoritzedWithPermissions(BatchCommon batchCommon) {
+ boolean result = true;
+
+ ResourceActionGroupList resourceActionGroupList = batchCommon.getResourceActionGroupList();
+ if (resourceActionGroupList != null) {
+ String tenantId = AuthN.get().getCurrentTenantId();
+ for (ResourceActionGroup resourceActionGroup: resourceActionGroupList.getResourceActionGroup()) {
+ String resourceName = resourceActionGroup.getResourceName();
+ ActionGroup actionGroup = ActionGroup.creatActionGroup(resourceActionGroup.getActionGroup());
+ for (ActionType actionType: actionGroup.getActions()) {
+ CSpaceResource res = new URIResourceImpl(tenantId, resourceName, AuthZ.getMethod(actionType));
+ if (AuthZ.get().isAccessAllowed(res) == false) {
+ return false;
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Returns a copy of the incoming list of strings all lower-cased. Also removes any duplicates.
+ *
+ * @param listOfStrings
+ * @return
+ */
+ private List<String> toLowerCase(List<String> listOfStrings) {
+ List<String> result = null;
+
+ if (listOfStrings != null) {
+ Set<String> stringSet = new HashSet<String>();
+ for (String s : listOfStrings) {
+ stringSet.add(s.toLowerCase());
+ }
+ result = new ArrayList<String>(stringSet);
+ }
+
+ return result;
+ }
- //
- // Acquire a repository session handle if we don't already have one
+ public InvocationResults invokeBatchJob(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String csid,
+ ResourceMap resourceMap, InvocationContext invocationCtx, BatchCommon batchCommon) throws Exception {
+ CoreSessionInterface repoSession = null;
boolean releaseRepoSession = false;
- CoreSessionInterface repoSession = this.getRepositorySession();
+
RepositoryClientImpl repoClient = (RepositoryClientImpl) this.getRepositoryClient(ctx);
- repoSession = this.getRepositorySession();
if (repoSession == null) {
repoSession = repoClient.getRepositorySession(ctx);
releaseRepoSession = true;
public class CreateAndLinkLoanOutBatchJob extends AbstractBatchInvocable {
- private static ArrayList<String> invocationModes = null;
- private InvocationContext invocationCtx;
- private int completionStatus;
- private InvocationResults results;
- private InvocationError errorInfo;
private final String RELATION_TYPE = "affects";
- private final String LOAN_DOCTYPE = "LoanOut";
+ private final String LOAN_DOCTYPE = "LoanOut";
private final String RELATION_PREDICATE_DISP = "affects";
- protected final int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
- protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
- protected final int INT_ERROR_STATUS = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
public CreateAndLinkLoanOutBatchJob() {
- CreateAndLinkLoanOutBatchJob.setupClassStatics();
- invocationCtx = null;
- completionStatus = STATUS_UNSTARTED;
- results = new InvocationResults();
- errorInfo = null;
- }
-
- private static void setupClassStatics() {
- if(invocationModes == null ) {
- invocationModes = new ArrayList<String>(1);
- invocationModes.add(INVOCATION_MODE_SINGLE);
- invocationModes.add(INVOCATION_MODE_LIST);
- }
- }
-
- /**
- * @return a set of modes that this plugin can support on invocation. Must be non-empty.
- */
- public List<String> getSupportedInvocationModes() {
- return CreateAndLinkLoanOutBatchJob.invocationModes;
+ setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST));
}
-
- @Override
- public InvocationContext getInvocationContext() {
- return invocationCtx;
- }
-
- /**
- * Sets the invocation context for the batch job. Called before run().
- * @param context an instance of InvocationContext.
- */
- @Override
- public void setInvocationContext(InvocationContext context) {
- this.invocationCtx = context;
- }
-
/**
* The main work logic of the batch job. Will be called after setContext.
*/