001: /*
002: * Copyright 2005-2007 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.help;
018:
019: import java.io.InputStream;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.jdom.Element;
024:
025: import edu.iu.uis.eden.EdenConstants;
026: import edu.iu.uis.eden.WorkflowServiceErrorException;
027: import edu.iu.uis.eden.WorkflowServiceErrorImpl;
028: import edu.iu.uis.eden.export.ExportDataSet;
029: import edu.iu.uis.eden.help.dao.HelpDAO;
030: import edu.iu.uis.eden.user.WorkflowUser;
031: import edu.iu.uis.eden.xml.export.HelpEntryXmlExporter;
032: import edu.iu.uis.eden.xml.help.HelpEntryXmlParser;
033:
034: public class HelpServiceImpl implements HelpService {
035: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
036: .getLogger(HelpServiceImpl.class);
037: private HelpDAO helpDAO;
038:
039: private static final String NAME_EMPTY = "helpentry.name.empty";
040: private static final String TEXT_EMPTY = "helpentry.text.empty";
041: private static final String ID_INVALID = "helpentry.id.invalid";
042: private static final String KEY_EMPTY = "helpentry.key.empty";
043: private static final String KEY_EXIST = "helpentry.key.exists";
044: private static final String KEY_ILLEGAL = "helpentry.key.illegal";
045:
046: public void save(HelpEntry helpEntry) {
047: validateHelpEntry(helpEntry);
048: getHelpDAO().save(helpEntry);
049: }
050:
051: public void saveXmlEntry(HelpEntry helpEntry) {
052: HelpEntry entry = validateXmlHelpEntry(helpEntry);
053: LOG.debug(entry.getHelpId() + ", " + entry.getHelpName() + ", "
054: + entry.getHelpText() + ", " + entry.getHelpKey());
055: getHelpDAO().save(entry);
056: }
057:
058: public void delete(HelpEntry helpEntry) {
059: getHelpDAO().deleteEntry(helpEntry);
060: }
061:
062: private HelpEntry validateXmlHelpEntry(HelpEntry helpEntry) {
063: LOG.debug("Enter validateXMLHelpEntry(..)");
064: List errors = new ArrayList();
065:
066: if (helpEntry.getHelpName() == null
067: || "".equals(helpEntry.getHelpName().trim())) {
068: errors.add(new WorkflowServiceErrorImpl("Help Name empty.",
069: NAME_EMPTY));
070: }
071: if (helpEntry.getHelpText() == null
072: || "".equals(helpEntry.getHelpText().trim())) {
073: errors.add(new WorkflowServiceErrorImpl("Help Text empty.",
074: TEXT_EMPTY));
075: } else {
076: helpEntry.setHelpText(helpEntry.getHelpText().trim());
077: }
078:
079: if (helpEntry.getHelpKey() == null
080: || "".equals(helpEntry.getHelpKey().trim())) {
081: errors.add(new WorkflowServiceErrorImpl("Help Key empty.",
082: KEY_EMPTY));
083: } else if (helpEntry.getHelpKey().indexOf("'") >= 0) {
084: errors.add(new WorkflowServiceErrorImpl(
085: "Help Key illegal character.", KEY_ILLEGAL, "'"));
086: } else {
087: helpEntry.setHelpKey(helpEntry.getHelpKey().trim());
088: HelpEntry entry1 = findByKey(helpEntry.getHelpKey());
089: if (helpEntry.getHelpId() == null && entry1 != null) {
090: Long id = entry1.getHelpId();
091: //LOG.debug("id of this help entry is: "+id.toString());
092: helpEntry.setHelpId(id);
093: helpEntry.setLockVerNbr(entry1.getLockVerNbr());
094: //LOG.debug(helpEntry.getHelpId()+", "+helpEntry.getHelpName()+", "+helpEntry.getHelpText()+", "+helpEntry.getHelpKey());
095: }
096: }
097:
098: LOG.debug("Exit validateXmlHelpEntry(..)");
099: if (!errors.isEmpty()) {
100: throw new WorkflowServiceErrorException(
101: "Help Entry validation Error", errors);
102: }
103: return helpEntry;
104: }
105:
106: private void validateHelpEntry(HelpEntry helpEntry) {
107: LOG.debug("Enter validateHelpEntry(..)");
108: List errors = new ArrayList();
109:
110: if (helpEntry.getHelpName() == null
111: || "".equals(helpEntry.getHelpName().trim())) {
112: errors.add(new WorkflowServiceErrorImpl("Help Name empty.",
113: NAME_EMPTY));
114: }
115: if (helpEntry.getHelpText() == null
116: || "".equals(helpEntry.getHelpText().trim())) {
117: errors.add(new WorkflowServiceErrorImpl("Help Text empty.",
118: TEXT_EMPTY));
119: } else {
120: helpEntry.setHelpText(helpEntry.getHelpText().trim());
121: }
122:
123: if (helpEntry.getHelpKey() == null
124: || "".equals(helpEntry.getHelpKey().trim())) {
125: errors.add(new WorkflowServiceErrorImpl("Help Key empty.",
126: KEY_EMPTY));
127: } else if (helpEntry.getHelpKey().indexOf("'") >= 0) {
128: errors.add(new WorkflowServiceErrorImpl(
129: "Help Key illegal character.", KEY_ILLEGAL, "'"));
130: } else {
131: helpEntry.setHelpKey(helpEntry.getHelpKey().trim());
132: if (helpEntry.getHelpId() == null
133: && findByKey(helpEntry.getHelpKey()) != null) {
134: errors.add(new WorkflowServiceErrorImpl(
135: "Help Key exists.", KEY_EXIST, helpEntry
136: .getHelpKey()));
137: }
138: }
139:
140: LOG.debug("Exit validateHelpEntry(..)");
141: if (!errors.isEmpty()) {
142: throw new WorkflowServiceErrorException(
143: "Help Entry validation Error", errors);
144: }
145: }
146:
147: public HelpEntry findByKey(String helpKey) {
148: return getHelpDAO().findByKey(helpKey);
149: }
150:
151: public HelpDAO getHelpDAO() {
152: return helpDAO;
153: }
154:
155: public void setHelpDAO(HelpDAO helpDAO) {
156: this .helpDAO = helpDAO;
157: }
158:
159: public HelpEntry findById(Long helpId) {
160: return getHelpDAO().findById(helpId);
161: }
162:
163: public List search(HelpEntry helpEntry) {
164: List errors = new ArrayList();
165: if (helpEntry.getHelpId() != null
166: && helpEntry.getHelpId().longValue() == 0) {
167: errors.add(new WorkflowServiceErrorImpl("Help Id invalid",
168: ID_INVALID));
169: }
170: if (!errors.isEmpty()) {
171: throw new WorkflowServiceErrorException(
172: "Help Entry search criteria error", errors);
173: }
174:
175: return getHelpDAO().search(helpEntry);
176: }
177:
178: public void loadXml(InputStream inputStream, WorkflowUser user) {
179: HelpEntryXmlParser parser = new HelpEntryXmlParser();
180: try {
181: parser.parseHelpEntries(inputStream);
182: } catch (Exception e) {
183: throw new WorkflowServiceErrorException(
184: "Error parsing help XML file",
185: new WorkflowServiceErrorImpl(
186: "Error parsing xml file.",
187: EdenConstants.XML_FILE_PARSE_ERROR));
188: }
189: }
190:
191: public Element export(ExportDataSet dataSet) {
192: HelpEntryXmlExporter exporter = new HelpEntryXmlExporter();
193: return exporter.export(dataSet);
194: }
195:
196: }
|