001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.module.chart.service.impl;
017:
018: import java.util.regex.Matcher;
019: import java.util.regex.Pattern;
020:
021: import org.apache.commons.beanutils.PropertyUtils;
022: import org.apache.commons.lang.StringUtils;
023: import org.kuali.core.bo.user.UniversalUser;
024: import org.kuali.core.exceptions.UserNotFoundException;
025: import org.kuali.core.service.impl.KualiModuleUserServiceBaseImpl;
026: import org.kuali.kfs.KFSConstants;
027: import org.kuali.kfs.context.SpringContext;
028: import org.kuali.kfs.service.ParameterService;
029: import org.kuali.kfs.service.impl.ParameterConstants;
030: import org.kuali.module.chart.bo.ChartUser;
031: import org.kuali.module.chart.service.ChartUserService;
032: import org.springframework.transaction.annotation.Transactional;
033:
034: /**
035: *
036: * This service implementation is the default implementation of the ChartUserService service that is delivered with Kuali.
037: */
038: @Transactional
039: public class ChartUserServiceImpl extends
040: KualiModuleUserServiceBaseImpl<ChartUser> implements
041: ChartUserService {
042: private static String administrationWorkgroupName;
043:
044: @SuppressWarnings("unused")
045: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
046: .getLogger(ChartUserServiceImpl.class);
047:
048: /**
049: *
050: * @see org.kuali.core.service.KualiModuleUserService#getModuleUser(org.kuali.core.bo.user.UniversalUser)
051: */
052: public ChartUser getModuleUser(UniversalUser universalUser)
053: throws UserNotFoundException {
054: ChartUser chartUser = new ChartUser(universalUser);
055: // KULRNE-4455 - removed from the "constructor" so that they can be lazy-loaded as needed
056: // chartUser.setAccountResponsibilities(accountService.getAccountsThatUserIsResponsibleFor(universalUser));
057: // chartUser.setChartResponsibilities(chartService.getChartsThatUserIsResponsibleFor(universalUser));
058:
059: return chartUser;
060: }
061:
062: /**
063: *
064: * @see org.kuali.module.chart.service.ChartUserService#isAdministratorUser(org.kuali.module.chart.bo.ChartUser)
065: */
066: public boolean isAdministratorUser(ChartUser user) {
067: if (administrationWorkgroupName == null) {
068: administrationWorkgroupName = SpringContext.getBean(
069: ParameterService.class).getParameterValue(
070: ParameterConstants.CHART_DOCUMENT.class,
071: KFSConstants.MAINTENANCE_ADMIN_WORKGROUP_PARM_NM);
072: }
073: return universalUserService.isMember(user.getUniversalUser(),
074: administrationWorkgroupName);
075: }
076:
077: /**
078: * Returns the chart code from the universal user as the default chart code.
079: *
080: * @see org.kuali.module.chart.service.ChartUserService#getDefaultChartCode(org.kuali.core.bo.user.UniversalUser)
081: */
082: public String getDefaultChartCode(UniversalUser user) {
083: return getDefaultCode(
084: user,
085: KFSConstants.ChartApcParms.DEFAULT_USER_CHART_CODE_SOURCE_ATTRIBUTE,
086: KFSConstants.ChartApcParms.DEFAULT_USER_CHART_CODE_EXTRACTION_REGEXP);
087: }
088:
089: /**
090: * Returns the primary department ID from the universal user table as the organization code. If the field is blank, then it
091: * substitutes the chart code.
092: *
093: * @see org.kuali.module.chart.service.ChartUserService#getDefaultOrganizationCode(org.kuali.core.bo.user.UniversalUser)
094: */
095: public String getDefaultOrganizationCode(UniversalUser user) {
096: return getDefaultCode(
097: user,
098: KFSConstants.ChartApcParms.DEFAULT_USER_ORGANIZATION_CODE_SOURCE_ATTRIBUTE,
099: KFSConstants.ChartApcParms.DEFAULT_USER_ORGANIZATION_CODE_EXTRACTION_REGEXP);
100: }
101:
102: /**
103: *
104: * This retrieves the default org or chart code for this user from the {@link ParameterService}
105: * @param user
106: * @param sourceAttributeParameterName
107: * @param extractionRegularExpressionParameterName
108: * @return default chart or org code as determined by the ParameterService
109: */
110: private String getDefaultCode(UniversalUser user,
111: String sourceAttributeParameterName,
112: String extractionRegularExpressionParameterName) {
113: String sourceAttribute = SpringContext.getBean(
114: ParameterService.class).getParameterValue(
115: ChartUser.class, sourceAttributeParameterName);
116: String extractionRegularExpression = SpringContext.getBean(
117: ParameterService.class).getParameterValue(
118: ChartUser.class,
119: extractionRegularExpressionParameterName);
120: String errorMessage = new StringBuffer(
121: "Unable to get default code for user: ").append(
122: user.getPersonUserIdentifier()).append(
123: " using sourceAttribute: ").append(sourceAttribute)
124: .append(" and regular expression: ").append(
125: extractionRegularExpression).toString();
126: String defaultCode = "";
127: if (user != null) {
128: try {
129: String sourceAttributeValue = (String) PropertyUtils
130: .getProperty(user, sourceAttribute);
131: if (sourceAttributeValue != null) {
132: if (StringUtils
133: .isBlank(extractionRegularExpression)) {
134: defaultCode = sourceAttributeValue;
135: } else {
136: Matcher extractionMatcher = Pattern.compile(
137: extractionRegularExpression).matcher(
138: sourceAttributeValue);
139: if (extractionMatcher.find()) {
140: return extractionMatcher.group();
141: }
142: LOG.warn(errorMessage);
143: }
144: }
145: } catch (Exception e) {
146: throw new RuntimeException(errorMessage, e);
147: }
148: }
149: return defaultCode;
150: }
151: }
|