1 18 19 package org.apache.roller.ui.authoring.struts.actions; 20 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.commons.lang.builder.HashCodeBuilder; 31 import org.apache.struts.action.ActionMapping; 32 import org.apache.roller.RollerException; 33 import org.apache.roller.model.RollerFactory; 34 import org.apache.roller.pojos.UserData; 35 import org.apache.roller.pojos.WeblogEntryData; 36 import org.apache.roller.ui.core.BasePageModel; 37 import org.apache.roller.ui.core.RollerRequest; 38 import org.apache.roller.ui.core.RollerSession; 39 import org.apache.roller.ui.authoring.struts.actions.WeblogEntryPageModel.PageMode; 40 import org.apache.roller.ui.authoring.struts.formbeans.WeblogEntryFormEx; 41 import org.apache.commons.lang.StringUtils; 42 43 import java.util.Map ; 45 import org.apache.commons.logging.Log; 46 import org.apache.commons.logging.LogFactory; 47 import org.apache.roller.config.RollerRuntimeConfig; 48 import org.apache.roller.model.PluginManager; 49 import org.apache.roller.model.Roller; 50 import org.apache.roller.pojos.WebsiteData; 51 52 56 public class WeblogEntryPageModel extends BasePageModel 57 { 58 private static Log logger = 59 LogFactory.getFactory().getInstance(WeblogEntryPageModel.class); 60 61 private RollerRequest rollerRequest = null; 62 private PageMode mode = null; 63 private ArrayList words = null; 64 private WeblogEntryFormEx form; 65 private WeblogEntryData weblogEntry; 66 67 public static class PageMode { 68 private String name; 69 public PageMode(String name) { 70 this.name = name; 71 } 72 public boolean equals(Object obj) { 73 return ((PageMode)obj).name.equals(name); 74 } 75 public int hashCode() { 76 return HashCodeBuilder.reflectionHashCode(this); 77 } 78 } 79 80 public static final PageMode EDIT_MODE = new PageMode("EDIT_MODE"); 81 public static final PageMode SPELL_MODE = new PageMode("SPELL_MODE"); 82 public static final PageMode PREVIEW_MODE = new PageMode("PREVIEW_MODE"); 83 84 85 public WeblogEntryPageModel( 86 HttpServletRequest request, 87 HttpServletResponse response, 88 ActionMapping mapping, 89 WeblogEntryFormEx form, 90 PageMode mode, 91 ArrayList words) throws RollerException 92 { 93 this(request, response, mapping, form, mode); 94 this.words = words; 95 } 96 97 public WeblogEntryPageModel( 98 HttpServletRequest request, 99 HttpServletResponse response, 100 ActionMapping mapping, 101 WeblogEntryFormEx form, 102 PageMode mode) throws RollerException 103 { 104 super("dummy", request, response, mapping); 105 this.rollerRequest = RollerRequest.getRollerRequest(request); 106 this.form = form; 107 this.mode = mode; 108 getRequest().setAttribute("leftPage","/weblog/WeblogEditSidebar.jsp"); 109 } 110 111 public WebsiteData getWeblog() { 112 return this.rollerRequest.getWebsite(); 113 } 114 115 public String getTitle() 116 { 117 if (StringUtils.isEmpty(form.getId())) 118 { 119 return bundle.getString("weblogEdit.title.newEntry"); 120 } 121 return bundle.getString("weblogEdit.title.editEntry"); 122 } 123 124 130 public List getRecentPublishedEntries() throws RollerException 131 { 132 RollerSession rollerSession = RollerSession.getRollerSession(getRequest()); 133 return RollerFactory.getRoller().getWeblogManager() 134 .getWeblogEntries( 135 getWeblogEntry().getWebsite(), null, 137 null, null, null, WeblogEntryData.PUBLISHED, null, null, 0, 20); 143 } 144 145 151 public List getRecentDraftEntries() throws RollerException 152 { 153 RollerSession rollerSession = RollerSession.getRollerSession(getRequest()); 154 return RollerFactory.getRoller().getWeblogManager() 155 .getWeblogEntries( 156 getWeblogEntry().getWebsite(), 157 null, 158 null, null, null, WeblogEntryData.DRAFT, "updateTime", null, 0, 20); } 165 166 172 public List getRecentPendingEntries() throws RollerException 173 { 174 RollerSession rollerSession = RollerSession.getRollerSession(getRequest()); 175 return RollerFactory.getRoller().getWeblogManager() 176 .getWeblogEntries( 177 getWeblogEntry().getWebsite(), 178 null, 179 null, null, null, WeblogEntryData.PENDING, "updateTime", null, 0, 20); 185 } 186 187 public List getHoursList() 188 { 189 List ret = new LinkedList (); 190 for (int i=0; i<24; i++) 191 { 192 ret.add(new Integer (i)); 193 } 194 return ret; 195 } 196 197 public List getMinutesList() 198 { 199 List ret = new LinkedList (); 200 for (int i=0; i<60; i++) 201 { 202 ret.add(new Integer (i)); 203 } 204 return ret; 205 } 206 207 public List getSecondsList() 208 { 209 return getMinutesList(); 210 } 211 212 public boolean getHasPagePlugins() 213 { 214 boolean ret = false; 215 try { 216 Roller roller = RollerFactory.getRoller(); 217 PluginManager ppmgr = roller.getPagePluginManager(); 218 ret = ppmgr.hasPagePlugins(); 219 } catch (RollerException e) { 220 logger.error(e); 221 } 222 return ret; 223 } 224 225 public List getPagePlugins() 226 { 227 List list = new ArrayList (); 228 try { 229 if (getHasPagePlugins()) 230 { 231 Roller roller = RollerFactory.getRoller(); 232 PluginManager ppmgr = roller.getPagePluginManager(); 233 Map plugins = ppmgr.getWeblogEntryPlugins( 234 getWebsite()); 235 Iterator it = plugins.values().iterator(); 236 while (it.hasNext()) list.add(it.next()); 237 } 238 } catch (Exception e) { 239 logger.error(e); 240 } 241 return list; 242 } 243 244 public String getEditorPage() 245 { 246 RollerSession rollerSession = RollerSession.getRollerSession(getRequest()); 249 String editorPage = weblogEntry.getWebsite().getEditorPage(); 250 if (StringUtils.isEmpty( editorPage )) 251 { 252 editorPage = "editor-text.jsp"; 253 } 254 return editorPage; 255 } 256 257 public UserData getUser() 258 { 259 RollerSession rollerSession = RollerSession.getRollerSession(getRequest()); 260 return rollerSession.getAuthenticatedUser(); 261 } 262 263 public List getCategories() throws Exception 264 { 265 RollerSession rollerSession = RollerSession.getRollerSession(getRequest()); 266 return RollerFactory.getRoller().getWeblogManager() 267 .getWeblogCategories(weblogEntry.getWebsite(), false); 268 } 269 270 public WeblogEntryFormEx getWeblogEntryForm() throws RollerException 271 { 272 return this.form; 273 } 274 275 276 public WeblogEntryData getWeblogEntry() throws RollerException 277 { 278 if (weblogEntry == null) 279 { 280 weblogEntry = new WeblogEntryData(); 281 weblogEntry.setWebsite(getWebsite()); 282 form.copyTo(weblogEntry, 283 getRequest().getLocale(), getRequest().getParameterMap()); 284 weblogEntry.setWebsite(weblogEntry.getWebsite()); 285 } 286 return weblogEntry; 287 } 288 289 public String getPermaLink() throws RollerException 290 { 291 String context = RollerRuntimeConfig.getAbsoluteContextURL(); 292 return context + getWeblogEntry().getPermaLink(); 293 } 294 295 public static String makeSelect(String word, List words) 296 { 297 StringBuffer buf = new StringBuffer ("<select name=\""); 298 buf.append("replacementWords\" style=\"font-size: 10px;\">"); 299 buf.append("<option selected=\"selected\" value=\"").append(word); 300 buf.append("\">").append(word).append("</option>"); 301 if (words == null || words.size() < 1) 302 { 303 buf.append("<option value=\"").append(word); 304 buf.append("\">No Suggestions</option>"); 305 } 306 else 307 { 308 for (Iterator it2=words.iterator(); it2.hasNext();) 309 { 310 word = it2.next().toString(); 311 buf.append("<option value=\"").append(word); 312 buf.append("\">").append(word).append("</option>"); 313 } 314 } 315 buf.append("</select>"); 316 return buf.toString(); 317 } 318 319 322 public PageMode getMode() { 323 return mode; 324 } 325 326 329 public void setMode(PageMode mode) { 330 this.mode = mode; 331 } 332 333 public boolean getEditMode() 334 { 335 return mode.equals(EDIT_MODE); 336 } 337 338 public boolean getSpellMode() 339 { 340 return mode.equals(SPELL_MODE); 341 } 342 343 public boolean getPreviewMode() 344 { 345 return mode.equals(PREVIEW_MODE); 346 } 347 348 351 public ArrayList getWords() { 352 return words; 353 } 354 357 public void setWords(ArrayList words) { 358 this.words = words; 359 } 360 361 public boolean getUserAuthorized() throws RollerException 362 { 363 return getRollerSession().isUserAuthorized(getWeblogEntry().getWebsite()); 364 } 365 366 public boolean getUserAuthorizedToAuthor() throws RollerException 367 { 368 return getRollerSession().isUserAuthorizedToAuthor(getWeblogEntry().getWebsite()); 369 } 370 371 public PageMode getEDIT_MODE() { 372 return EDIT_MODE; 373 } 374 375 public int getCommentCount() { 376 if (weblogEntry.getId() == null) return 0; 378 List comments = comments = weblogEntry.getComments(false, false); 379 return comments != null ? comments.size() : 0; 380 } 381 } 382 383 | Popular Tags |