1 4 package com.openedit.modules.workflow; 5 6 import java.io.File ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.dom4j.Element; 13 import org.openedit.repository.ContentItem; 14 15 import com.openedit.OpenEditException; 16 import com.openedit.page.FileFinder; 17 import com.openedit.page.Page; 18 import com.openedit.page.PageProperty; 19 import com.openedit.page.manage.PageManager; 20 import com.openedit.users.User; 21 import com.openedit.users.UserManager; 22 import com.openedit.util.XmlUtil; 23 24 public class WorkFlow 25 { 26 protected File fieldRoot; 27 protected PageManager fieldPageManager; 28 protected UserManager fieldUserManager; 29 protected int fieldLevelCount = 0; 30 private static final Log log = LogFactory.getLog(WorkFlow.class); 31 protected long fieldLastModified = -2; 32 public List listAllDrafts() throws OpenEditException 33 { 34 FileFinder finder = new FileFinder(); 35 finder.setRoot(getRoot()); 36 finder.setPageManager(getPageManager()); 37 finder.addSkipFileName("*.xconf"); 38 finder.setUseMatches(true); 39 return finder.findPages("*.draft.*"); 40 } 41 public String getUserDescription( String inUserName ) 42 { 43 if ( inUserName == null || inUserName.length() == 0) 44 { 45 return ""; 46 } 47 User user = getUserManager().getUser(inUserName); 48 if ( user != null) 49 { 50 return user.getShortDescription(); 51 } 52 return ""; 53 } 54 public ContentItem getLastVersion(Page inDraft ) throws OpenEditException 55 { 56 ContentItem revision = getPageManager().getLatestVersion(inDraft.getPath()); 57 if( revision != null) 58 { 59 return revision; 60 } 61 return inDraft.getContentItem(); 62 63 } 64 public Page getOriginalPage(Page inDraft) throws OpenEditException 65 { 66 String dpath = inDraft.getPath(); 67 int index = dpath.indexOf(".draft."); 68 String path = dpath.substring(0,index); 69 path = path + dpath.substring(index+6); 70 71 Page org = getPageManager().getPage(path); 72 return org; 73 } 74 75 public PageManager getPageManager() 76 { 77 return fieldPageManager; 78 } 79 public void setPageManager(PageManager inPageManager) 80 { 81 fieldPageManager = inPageManager; 82 } 83 public File getRoot() 84 { 85 return fieldRoot; 86 } 87 public void setRoot(File inRoot) 88 { 89 fieldRoot = inRoot; 90 } 91 public UserManager getUserManager() 92 { 93 return fieldUserManager; 94 } 95 public void setUserManager(UserManager inUserManager) 96 { 97 fieldUserManager = inUserManager; 98 } 99 public void approve(String inPath, User inUser) throws Exception 100 { 101 Page draft = getPageManager().getPage(inPath); 102 if(!draft.exists()) 103 { 104 log.error("No draft found to approve " + inPath); 105 return; 106 } 107 loadLevelCount(); 109 110 if(getLevelCount() > 0) 112 { 113 int newLevel = findHighestApproval( inUser ); 114 if( newLevel > 0) 115 { 116 int oldLevel = findExistingLevel(draft); 117 if( oldLevel < newLevel) 118 { 119 saveLevel(draft, newLevel); 120 } 121 } 122 if( newLevel < getLevelCount()) { 124 log.info("Requires more approval"); 125 return; 126 } 127 } 128 String orig = inPath.replaceAll("\\.draft", ""); 129 Page dest = getPageManager().getPage(orig); 130 131 dest.getContentItem().setAuthor(inUser.getUserName()); 132 133 ContentItem rev = getLastVersion(draft); 134 dest.getContentItem().setMessage("Approved: (" + rev.getAuthor() +" "+ rev.getMessage() +")"); 135 dest.getContentItem().setType( ContentItem.TYPE_APPROVED); 136 137 getPageManager().movePage(draft, dest); 138 if( getLevelCount() > 0) 139 { 140 saveLevel(draft, 0); 141 } 142 143 } 144 private void saveLevel(Page draft, int newLevel) throws OpenEditException 145 { 146 PageProperty property = new PageProperty("approve.level"); 147 property.setValue(String.valueOf(newLevel )); 148 draft.getPageSettings().putProperty(property); 149 getPageManager().getPageSettingsManager().saveSetting(draft.getPageSettings()); 151 } 152 protected void loadLevelCount() throws OpenEditException 153 { 154 Page level = getPageManager().getPage("/openedit/workflow/settings.xml"); 155 if( level.getLastModified().getTime() != fieldLastModified) 156 { 157 fieldLastModified = level.getLastModified().getTime(); 158 Element root = new XmlUtil().getXml(level.getReader(), "UTF-8"); 159 String text = root.elementText("levels"); 160 if( text != null) 161 { 162 setLevelCount(Integer.parseInt(text)); 163 } 164 } 165 } 166 public int findExistingLevel( Page inDraft) 167 { 168 String existingLevel = inDraft.get("approve.level"); 169 int oldLevel = 0; 170 if( existingLevel != null) 171 { 172 oldLevel = Integer.parseInt(existingLevel); 173 } 174 return oldLevel; 175 } 176 public boolean canApprove( User inUser, Page inDraft) throws OpenEditException 177 { 178 if( inDraft.isDraft() && !inDraft.exists()) 179 { 180 return false; 181 } 182 loadLevelCount(); 183 if( getLevelCount() == 0) 184 { 185 if( inUser.hasPermission("oe.edit.approves") ) 186 { 187 return true; 188 } 189 else 190 { 191 return false; 192 } 193 } 194 int power = findHighestApproval(inUser); 195 if( power == 0) 196 { 197 return false; 198 } 199 int existing = findExistingLevel(inDraft); 200 if( power > existing) 201 { 202 return true; 203 } 204 return false; 205 } 206 207 public int findHighestApproval(User inUser ) 208 { 209 int level = 0; 210 for (int i = 1; i < getLevelCount()+1; i++) 211 { 212 if( inUser.hasPermission("oe.edit.approve.level" + i) ) 213 { 214 level = i; 215 } 216 } 217 return level; 218 } 219 public void approveAll(User inUser) throws Exception 220 { 221 List pages = listAllDrafts(); 222 for (Iterator iter = pages.iterator(); iter.hasNext();) 223 { 224 Page editPath = (Page) iter.next(); 225 approve(editPath.getPath(), inUser); 226 } 227 } 228 public void deleteDraft(String inPath, User inUser) throws OpenEditException 229 { 230 Page page = getPageManager().getPage(inPath); 231 page.getContentItem().setAuthor(inUser.getUserName()); 232 page.getContentItem().setMessage("Deleted Draft"); 233 getPageManager().removePage(page); 234 } 235 public int getLevelCount() 236 { 237 return fieldLevelCount; 238 } 239 public void setLevelCount(int inLevelCount) 240 { 241 fieldLevelCount = inLevelCount; 242 } 243 244 } 245 | Popular Tags |