001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kns.web.struts.form;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreApiServiceLocator;
020import org.kuali.rice.core.api.encryption.EncryptionService;
021import org.kuali.rice.kns.service.BusinessObjectAuthorizationService;
022import org.kuali.rice.kns.service.KNSServiceLocator;
023import org.kuali.rice.krad.bo.BusinessObject;
024
025import javax.servlet.http.HttpServletRequest;
026import java.security.GeneralSecurityException;
027import java.util.HashMap;
028import java.util.Iterator;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * This is a description of what this class does - wliang don't forget to fill this in. 
034 * 
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public class DisplayInactivationBlockersForm extends KualiForm {
039    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisplayInactivationBlockersForm.class);
040
041        private String businessObjectClassName;
042        private Map<String, String> primaryKeyFieldValues;
043        private Map<String, List<String>> blockingValues;
044        
045        @Override
046        public void populate(HttpServletRequest request) {
047                super.populate(request);
048                primaryKeyFieldValues = new HashMap<String, String>();
049
050                if (StringUtils.isBlank(businessObjectClassName)) {
051                        throw new IllegalArgumentException("BO Class Name missing.");
052                }
053                
054                Class businessObjectClass = null;
055                try {
056                        businessObjectClass = Class.forName(businessObjectClassName);
057                } catch (ClassNotFoundException e) {
058                        LOG.error("Unable to find class: " + businessObjectClassName, e);
059                        throw new IllegalArgumentException("Unable to find class: " + businessObjectClassName, e);
060                }
061                
062                if (!BusinessObject.class.isAssignableFrom(businessObjectClass)) {
063                        LOG.error("BO Class is not a BusinessObject: " + businessObjectClassName);
064                        throw new IllegalArgumentException("BO Class is not a BusinessObject: " + businessObjectClassName);
065                }
066                
067                EncryptionService encryptionService = CoreApiServiceLocator.getEncryptionService();
068                BusinessObjectAuthorizationService businessObjectAuthorizationService = KNSServiceLocator
069                .getBusinessObjectAuthorizationService();
070                
071                List primaryKeyFieldNames = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(businessObjectClass);
072                for (Iterator i = primaryKeyFieldNames.iterator(); i.hasNext();) {
073                        String primaryKeyFieldName = (String) i.next();
074                        
075                        String primaryKeyFieldValue = request.getParameter(primaryKeyFieldName);
076                        if (StringUtils.isBlank(primaryKeyFieldValue)) {
077                                LOG.error("Missing primary key value for: " + primaryKeyFieldName);
078                                throw new IllegalArgumentException("Missing primary key value for: " + primaryKeyFieldName);
079                        }
080                        
081            // check if field is a secure
082            if (businessObjectAuthorizationService.attributeValueNeedsToBeEncryptedOnFormsAndLinks(businessObjectClass, primaryKeyFieldName)) {
083                try {
084                    if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
085                            primaryKeyFieldValue = encryptionService.decrypt(primaryKeyFieldValue);
086                    }
087                }
088                catch (GeneralSecurityException e) {
089                    LOG.error("Unable to decrypt secure field for BO " + businessObjectClassName + " field " + primaryKeyFieldName, e);
090                    throw new RuntimeException("Unable to decrypt secure field for BO " + businessObjectClassName + " field " + primaryKeyFieldName, e);
091                }
092            }
093            
094                        primaryKeyFieldValues.put(primaryKeyFieldName, primaryKeyFieldValue);
095                }
096        }
097
098        public String getBusinessObjectClassName() {
099                return this.businessObjectClassName;
100        }
101
102        public void setBusinessObjectClassName(String businessObjectClassName) {
103                this.businessObjectClassName = businessObjectClassName;
104        }
105
106        public Map<String, String> getPrimaryKeyFieldValues() {
107                return this.primaryKeyFieldValues;
108        }
109
110        public void setPrimaryKeyFieldValues(Map<String, String> primaryKeyFieldValues) {
111                this.primaryKeyFieldValues = primaryKeyFieldValues;
112        }
113
114        public Map<String, List<String>> getBlockingValues() {
115                return this.blockingValues;
116        }
117
118        public void setBlockingValues(Map<String, List<String>> blockingValues) {
119                this.blockingValues = blockingValues;
120        }       
121}