|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.kew.help.service.impl;  | 
  |  18 |     | 
     | 
  |  19 |     | 
   import java.io.InputStream;  | 
  |  20 |     | 
   import java.util.ArrayList;  | 
  |  21 |     | 
   import java.util.List;  | 
  |  22 |     | 
     | 
  |  23 |     | 
   import org.jdom.Element;  | 
  |  24 |     | 
   import org.kuali.rice.core.api.impex.ExportDataSet;  | 
  |  25 |     | 
   import org.kuali.rice.core.framework.persistence.jta.TransactionalNoValidationExceptionRollback;  | 
  |  26 |     | 
   import org.kuali.rice.kew.exception.WorkflowServiceErrorException;  | 
  |  27 |     | 
   import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;  | 
  |  28 |     | 
   import org.kuali.rice.kew.help.HelpEntry;  | 
  |  29 |     | 
   import org.kuali.rice.kew.help.dao.HelpDAO;  | 
  |  30 |     | 
   import org.kuali.rice.kew.help.service.HelpService;  | 
  |  31 |     | 
   import org.kuali.rice.kew.util.KEWConstants;  | 
  |  32 |     | 
   import org.kuali.rice.kew.xml.HelpEntryXmlParser;  | 
  |  33 |     | 
   import org.kuali.rice.kew.xml.export.HelpEntryXmlExporter;  | 
  |  34 |     | 
   import org.kuali.rice.kns.exception.ValidationException;  | 
  |  35 |     | 
   import org.kuali.rice.kns.util.GlobalVariables;  | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
   @TransactionalNoValidationExceptionRollback  | 
  |  39 |    0 |    public class HelpServiceImpl implements HelpService { | 
  |  40 |    0 |        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HelpServiceImpl.class);  | 
  |  41 |     | 
       private HelpDAO helpDAO;  | 
  |  42 |     | 
     | 
  |  43 |     | 
       private static final String HELP_NAME_KEY = "helpEntry.helpName";  | 
  |  44 |     | 
       private static final String HELP_KEY_KEY = "helpEntry.helpKey";  | 
  |  45 |     | 
       private static final String HELP_TEXT_KEY = "helpEntry.helpText";  | 
  |  46 |     | 
       private static final String HELP_ID_KEY = "helpEntry.helpId";  | 
  |  47 |     | 
       private static final String ID_INVALID = "helpentry.id.invalid";  | 
  |  48 |     | 
       private static final String NAME_EMPTY = "helpentry.name.empty";  | 
  |  49 |     | 
       private static final String TEXT_EMPTY = "helpentry.text.empty";  | 
  |  50 |     | 
       private static final String KEY_EMPTY = "helpentry.key.empty";  | 
  |  51 |     | 
       private static final String KEY_EXIST = "helpentry.key.exists";  | 
  |  52 |     | 
       private static final String KEY_ILLEGAL = "helpentry.key.illegal";  | 
  |  53 |     | 
     | 
  |  54 |     | 
       public void save(HelpEntry helpEntry){ | 
  |  55 |    0 |            validateHelpEntry(helpEntry);  | 
  |  56 |    0 |            getHelpDAO().save(helpEntry);  | 
  |  57 |    0 |        }  | 
  |  58 |     | 
     | 
  |  59 |     | 
       public void saveXmlEntry(HelpEntry helpEntry){ | 
  |  60 |    0 |                HelpEntry entry=validateXmlHelpEntry(helpEntry);  | 
  |  61 |    0 |                LOG.debug(entry.getHelpId()+", "+entry.getHelpName()+", "+entry.getHelpText()+", "+entry.getHelpKey());  | 
  |  62 |    0 |                getHelpDAO().save(entry);  | 
  |  63 |    0 |        }  | 
  |  64 |     | 
     | 
  |  65 |     | 
       public void delete(HelpEntry helpEntry){ | 
  |  66 |    0 |                getHelpDAO().deleteEntry(helpEntry);  | 
  |  67 |    0 |        }  | 
  |  68 |     | 
     | 
  |  69 |     | 
       private HelpEntry validateXmlHelpEntry(HelpEntry helpEntry){ | 
  |  70 |    0 |                LOG.debug("Enter validateXMLHelpEntry(..)"); | 
  |  71 |     | 
     | 
  |  72 |    0 |            if (helpEntry.getHelpName() == null || "".equals(helpEntry.getHelpName().trim())) { | 
  |  73 |    0 |                GlobalVariables.getMessageMap().putError(HELP_NAME_KEY, NAME_EMPTY);  | 
  |  74 |     | 
           }  | 
  |  75 |    0 |            if (helpEntry.getHelpText() == null || "".equals(helpEntry.getHelpText().trim())) { | 
  |  76 |    0 |                GlobalVariables.getMessageMap().putError(HELP_TEXT_KEY, TEXT_EMPTY);  | 
  |  77 |     | 
           } else { | 
  |  78 |    0 |                helpEntry.setHelpText(helpEntry.getHelpText().trim());  | 
  |  79 |     | 
           }  | 
  |  80 |     | 
     | 
  |  81 |    0 |            if (helpEntry.getHelpKey() == null || "".equals(helpEntry.getHelpKey().trim())) { | 
  |  82 |    0 |                GlobalVariables.getMessageMap().putError(HELP_KEY_KEY, KEY_EMPTY);  | 
  |  83 |    0 |            } else if (helpEntry.getHelpKey().contains("'")) { | 
  |  84 |    0 |                GlobalVariables.getMessageMap().putError(HELP_KEY_KEY, KEY_ILLEGAL, "'");  | 
  |  85 |     | 
           } else { | 
  |  86 |    0 |                helpEntry.setHelpKey(helpEntry.getHelpKey().trim());  | 
  |  87 |    0 |                HelpEntry entry1=findByKey(helpEntry.getHelpKey());  | 
  |  88 |    0 |                if(helpEntry.getHelpId() == null && entry1 != null){ | 
  |  89 |    0 |                        Long id=entry1.getHelpId();  | 
  |  90 |     | 
                         | 
  |  91 |    0 |                        helpEntry.setHelpId(id);  | 
  |  92 |    0 |                    helpEntry.setLockVerNbr(entry1.getLockVerNbr());  | 
  |  93 |     | 
                         | 
  |  94 |     | 
               }  | 
  |  95 |     | 
           }  | 
  |  96 |     | 
     | 
  |  97 |    0 |            LOG.debug("Exit validateXmlHelpEntry(..)"); | 
  |  98 |    0 |            if (GlobalVariables.getMessageMap().hasErrors()) { | 
  |  99 |    0 |                throw new ValidationException("errors in help"); | 
  |  100 |     | 
           }  | 
  |  101 |    0 |            return helpEntry;  | 
  |  102 |     | 
       }  | 
  |  103 |     | 
     | 
  |  104 |     | 
     | 
  |  105 |     | 
       private void validateHelpEntry(HelpEntry helpEntry){ | 
  |  106 |    0 |            LOG.debug("Enter validateHelpEntry(..)"); | 
  |  107 |    0 |            List errors = new ArrayList();  | 
  |  108 |     | 
     | 
  |  109 |    0 |            if (helpEntry.getHelpName() == null || "".equals(helpEntry.getHelpName().trim())) { | 
  |  110 |    0 |                GlobalVariables.getMessageMap().putError(HELP_NAME_KEY, NAME_EMPTY);  | 
  |  111 |     | 
           }  | 
  |  112 |    0 |            if (helpEntry.getHelpText() == null || "".equals(helpEntry.getHelpText().trim())) { | 
  |  113 |    0 |                GlobalVariables.getMessageMap().putError(HELP_TEXT_KEY, TEXT_EMPTY);  | 
  |  114 |     | 
           } else { | 
  |  115 |    0 |                helpEntry.setHelpText(helpEntry.getHelpText().trim());  | 
  |  116 |     | 
           }  | 
  |  117 |     | 
     | 
  |  118 |    0 |            if (helpEntry.getHelpKey() == null || "".equals(helpEntry.getHelpKey().trim())) { | 
  |  119 |    0 |                GlobalVariables.getMessageMap().putError(HELP_KEY_KEY, KEY_EMPTY);  | 
  |  120 |    0 |            } else if (helpEntry.getHelpKey().contains("'")){ | 
  |  121 |    0 |                GlobalVariables.getMessageMap().putError(HELP_KEY_KEY, KEY_ILLEGAL, "'");  | 
  |  122 |     | 
           } else { | 
  |  123 |    0 |                helpEntry.setHelpKey(helpEntry.getHelpKey().trim());  | 
  |  124 |    0 |                if(helpEntry.getHelpId() == null && findByKey(helpEntry.getHelpKey()) != null){ | 
  |  125 |    0 |                    GlobalVariables.getMessageMap().putError(HELP_KEY_KEY, KEY_EXIST, helpEntry.getHelpKey());  | 
  |  126 |     | 
               }  | 
  |  127 |     | 
           }  | 
  |  128 |     | 
     | 
  |  129 |    0 |            LOG.debug("Exit validateHelpEntry(..)"); | 
  |  130 |    0 |            if (GlobalVariables.getMessageMap().hasErrors()) { | 
  |  131 |    0 |                throw new ValidationException("errors in help"); | 
  |  132 |     | 
           }  | 
  |  133 |    0 |        }  | 
  |  134 |     | 
     | 
  |  135 |     | 
       public HelpEntry findByKey(String helpKey){ | 
  |  136 |    0 |            return getHelpDAO().findByKey(helpKey);  | 
  |  137 |     | 
       }  | 
  |  138 |     | 
     | 
  |  139 |     | 
       public HelpDAO getHelpDAO() { | 
  |  140 |    0 |            return helpDAO;  | 
  |  141 |     | 
       }  | 
  |  142 |     | 
       public void setHelpDAO(HelpDAO helpDAO) { | 
  |  143 |    0 |            this.helpDAO = helpDAO;  | 
  |  144 |    0 |        }  | 
  |  145 |     | 
     | 
  |  146 |     | 
       public HelpEntry findById(Long helpId){ | 
  |  147 |    0 |            return getHelpDAO().findById(helpId);  | 
  |  148 |     | 
       }  | 
  |  149 |     | 
     | 
  |  150 |     | 
       public List search(HelpEntry helpEntry){ | 
  |  151 |    0 |            if(helpEntry.getHelpId() != null && helpEntry.getHelpId().longValue() == 0){ | 
  |  152 |    0 |                GlobalVariables.getMessageMap().putError(HELP_ID_KEY, ID_INVALID);  | 
  |  153 |     | 
           }  | 
  |  154 |    0 |            if (GlobalVariables.getMessageMap().hasErrors()) { | 
  |  155 |    0 |                throw new ValidationException("errors in help"); | 
  |  156 |     | 
           }  | 
  |  157 |     | 
     | 
  |  158 |    0 |            return getHelpDAO().search(helpEntry);  | 
  |  159 |     | 
       }  | 
  |  160 |     | 
     | 
  |  161 |     | 
       public void loadXml(InputStream inputStream, String principalId){ | 
  |  162 |    0 |            HelpEntryXmlParser parser = new HelpEntryXmlParser();  | 
  |  163 |     | 
           try { | 
  |  164 |    0 |                parser.parseHelpEntries(inputStream);  | 
  |  165 |    0 |            } catch(Exception e){ | 
  |  166 |    0 |                throw new WorkflowServiceErrorException("Error parsing help  XML file", new WorkflowServiceErrorImpl("Error parsing xml file.", KEWConstants.XML_FILE_PARSE_ERROR) ); | 
  |  167 |    0 |                }  | 
  |  168 |    0 |        }  | 
  |  169 |     | 
     | 
  |  170 |     | 
           public Element export(ExportDataSet dataSet) { | 
  |  171 |    0 |                    HelpEntryXmlExporter exporter = new HelpEntryXmlExporter();  | 
  |  172 |    0 |                    return exporter.export(dataSet);  | 
  |  173 |     | 
           }  | 
  |  174 |     | 
     | 
  |  175 |     | 
           @Override  | 
  |  176 |     | 
           public boolean supportPrettyPrint() { | 
  |  177 |    0 |                    return true;  | 
  |  178 |     | 
           }  | 
  |  179 |     | 
     | 
  |  180 |     | 
   }  |