001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.applicationconstants;
018:
019: import java.io.InputStream;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.apache.log4j.Logger;
024:
025: import edu.iu.uis.eden.EdenConstants;
026: import edu.iu.uis.eden.KEWServiceLocator;
027: import edu.iu.uis.eden.WorkflowServiceError;
028: import edu.iu.uis.eden.WorkflowServiceErrorException;
029: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
030: import edu.iu.uis.eden.applicationconstants.dao.ApplicationConstantsDAO;
031: import edu.iu.uis.eden.applicationconstants.xml.ApplicationConstantsXmlParser;
032: import edu.iu.uis.eden.user.WorkflowUser;
033:
034: /**
035: * Default implementation of the {@link ApplicationConstantsService}.
036: *
037: * @author natjohns
038: * @author xqi
039: */
040: public class ApplicationConstantsServiceImpl implements
041: ApplicationConstantsService {
042: private static final Logger LOG = Logger
043: .getLogger(ApplicationConstantsServiceImpl.class);
044:
045: private static final String NAME_EMPTY = "applicationconstants.error.emptyname";
046:
047: private static final String NAME_HAS_WHITE_SPACE = "applicationconstants.error.namehaswhitespace";
048:
049: private static final String VALUE_EMPTY = "applicationconstants.error.emptyvalue";
050:
051: private ApplicationConstantsDAO applicationConstantsDAO;
052:
053: public void save(ApplicationConstant applicationConstant) {
054: validateApplicationConstant(applicationConstant);
055: versionBlankConstant(applicationConstant);
056: getApplicationConstantsDAO().saveConstant(applicationConstant);
057: KEWServiceLocator.getCacheAdministrator().flushEntry(
058: applicationConstant.getApplicationConstantName());
059: putConstantInCache(applicationConstant);
060: }
061:
062: /**
063: * Allows passing in of a new ApplicationConstant object with an existing constant value with first
064: * having to fetch that constant.
065: *
066: * @param applicationConstant that will be saved
067: */
068: private void versionBlankConstant(
069: ApplicationConstant applicationConstant) {
070: if (applicationConstant.getLockVerNbr() == null) {
071: ApplicationConstant fetchedConstant = findByName(applicationConstant
072: .getApplicationConstantName());
073: if (fetchedConstant != null) {
074: applicationConstant.setLockVerNbr(fetchedConstant
075: .getLockVerNbr());
076: }
077: }
078: }
079:
080: public void delete(ApplicationConstant applicationConstant) {
081: getApplicationConstantsDAO()
082: .deleteConstant(applicationConstant);
083: KEWServiceLocator.getCacheAdministrator().flushEntry(
084: applicationConstant.getApplicationConstantName());
085: }
086:
087: public ApplicationConstant findByName(String applicationConstantName) {
088: ApplicationConstant constant = (ApplicationConstant) KEWServiceLocator
089: .getCacheAdministrator().getFromCache(
090: applicationConstantName);
091: if (constant == null) {
092: constant = getApplicationConstantsDAO().findByName(
093: applicationConstantName);
094: if (constant != null) {
095: putConstantInCache(constant);
096: }
097: }
098: return constant;
099: }
100:
101: public List<ApplicationConstant> findAll() {
102: List<ApplicationConstant> appConstants = getApplicationConstantsDAO()
103: .findAll();
104: for (ApplicationConstant constant : appConstants) {
105: putConstantInCache(constant);
106: }
107: return appConstants;
108: }
109:
110: private void putConstantInCache(ApplicationConstant constant) {
111: KEWServiceLocator.getCacheAdministrator().putInCache(
112: constant.getApplicationConstantName(), constant,
113: APPLICATION_CONSTANTS_CACHE_ID);
114: }
115:
116: private void validateApplicationConstant(
117: ApplicationConstant constant) {
118: LOG.debug("Enter validateApplicationConstant(..)...");
119: List<WorkflowServiceError> errors = new ArrayList<WorkflowServiceError>();
120:
121: if (constant.getApplicationConstantName() == null
122: || "".equals(constant.getApplicationConstantName()
123: .trim())) {
124: errors.add(new WorkflowServiceErrorImpl(
125: "Application constant name is empty.", NAME_EMPTY));
126: } else {
127: constant.setApplicationConstantName(constant
128: .getApplicationConstantName().trim());
129: }
130:
131: if (constant.getApplicationConstantName() != null
132: && constant.getApplicationConstantName().trim()
133: .indexOf(" ") > 0) {
134: errors.add(new WorkflowServiceErrorImpl(
135: "Application constant name has white space(s)",
136: NAME_HAS_WHITE_SPACE));
137: }
138:
139: if (constant.getApplicationConstantValue() == null
140: || "".equals(constant.getApplicationConstantValue()
141: .trim())) {
142: errors
143: .add(new WorkflowServiceErrorImpl(
144: "Application constant value is empty.",
145: VALUE_EMPTY));
146: } else {
147: constant.setApplicationConstantValue(constant
148: .getApplicationConstantValue().trim());
149: }
150:
151: LOG.debug("Exit validateApplicationConstant(..) ");
152: if (!errors.isEmpty()) {
153: throw new WorkflowServiceErrorException(
154: "Application Constant Validation Error", errors);
155: }
156: }
157:
158: public void loadXml(InputStream inputStream, WorkflowUser user) {
159: ApplicationConstantsXmlParser parser = new ApplicationConstantsXmlParser();
160: try {
161: parser.parseAppConstEntries(inputStream);
162: } catch (Exception e) {
163: WorkflowServiceErrorException wsee = new WorkflowServiceErrorException(
164: "Error parsing Application Constants XML file",
165: new WorkflowServiceErrorImpl(
166: "Error parsing XML file.",
167: EdenConstants.XML_FILE_PARSE_ERROR));
168: wsee.initCause(e);
169: throw wsee;
170: }
171: }
172:
173: /**
174: * @return applicationConstantsDAO
175: */
176: public ApplicationConstantsDAO getApplicationConstantsDAO() {
177: return applicationConstantsDAO;
178: }
179:
180: /**
181: * @param historyDAO
182: */
183: public void setApplicationConstantsDAO(
184: ApplicationConstantsDAO historyDAO) {
185: applicationConstantsDAO = historyDAO;
186: }
187: }
|