1 4 package org.roller.presentation.weblog.actions; 5 6 import com.swabunga.spell.event.SpellCheckEvent; 7 8 import org.apache.commons.lang.builder.HashCodeBuilder; 9 import org.apache.struts.action.ActionForward; 10 import org.apache.struts.action.ActionMapping; 11 import org.roller.RollerException; 12 import org.roller.model.WeblogManager; 13 import org.roller.pojos.UserData; 14 import org.roller.pojos.WeblogEntryData; 15 import org.roller.presentation.BasePageModel; 16 import org.roller.presentation.RollerContext; 17 import org.roller.presentation.RollerRequest; 18 import org.roller.presentation.tags.calendar.CalendarModel; 19 import org.roller.presentation.velocity.ContextLoader; 20 import org.roller.presentation.weblog.formbeans.WeblogEntryFormEx; 21 import org.roller.presentation.weblog.tags.EditWeblogCalendarModel; 22 import org.roller.util.StringUtils; 23 24 import java.util.ArrayList ; 25 import java.util.Date ; 26 import java.util.Iterator ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 import java.util.ListIterator ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 38 public class WeblogEntryPageModel extends BasePageModel 39 { 40 private RollerRequest rollerRequest = null; 41 private PageMode mode = null; 42 private ArrayList words = null; 43 private WeblogEntryFormEx form; 44 private List comments = null; 45 private WeblogEntryData weblogEntry; 46 47 public static class PageMode { 48 private String name; 49 public PageMode(String name) { 50 this.name = name; 51 } 52 public boolean equals(Object obj) { 53 return ((PageMode)obj).name.equals(name); 54 } 55 public int hashCode() { 56 return HashCodeBuilder.reflectionHashCode(this); 57 } 58 } 59 60 public static final PageMode EDIT_MODE = new PageMode("EDIT_MODE"); 61 public static final PageMode SPELL_MODE = new PageMode("SPELL_MODE"); 62 public static final PageMode PREVIEW_MODE = new PageMode("PREVIEW_MODE"); 63 64 65 public WeblogEntryPageModel( 66 HttpServletRequest request, 67 HttpServletResponse response, 68 ActionMapping mapping, 69 WeblogEntryFormEx form, 70 PageMode mode, 71 ArrayList words) throws RollerException 72 { 73 this(request, response, mapping, form, mode); 74 this.words = words; 75 } 76 77 public WeblogEntryPageModel( 78 HttpServletRequest request, 79 HttpServletResponse response, 80 ActionMapping mapping, 81 WeblogEntryFormEx form, 82 PageMode mode) throws RollerException 83 { 84 super(request, response, mapping); 85 this.rollerRequest = RollerRequest.getRollerRequest(request); 86 this.form = form; 87 this.mode = mode; 88 89 getRequest().setAttribute("leftPage","/weblog/WeblogEditSidebar.jsp"); 90 91 if (null != form.getId()) 92 { 93 WeblogManager wmgr = rollerRequest.getRoller().getWeblogManager(); 94 comments = wmgr.getComments(form.getId(), false); 95 } 96 } 97 98 public String getBaseURL() 99 { 100 return getRequest().getContextPath(); 101 } 102 103 109 public List getRecentPublishedEntries() throws RollerException 110 { 111 return rollerRequest.getRoller().getWeblogManager() 112 .getWeblogEntries( 113 rollerRequest.getWebsite(), null, null, null, WeblogManager.PUB_ONLY, new Integer (20)); } 120 121 127 public List getRecentDraftEntries() throws RollerException 128 { 129 return rollerRequest.getRoller().getWeblogManager() 130 .getWeblogEntries( 131 rollerRequest.getWebsite(), 132 null, null, null, WeblogManager.DRAFT_ONLY, new Integer (20)); } 138 139 public List getHoursList() 140 { 141 List ret = new LinkedList (); 142 for (int i=0; i<24; i++) 143 { 144 ret.add(new Integer (i)); 145 } 146 return ret; 147 } 148 149 public List getMinutesList() 150 { 151 List ret = new LinkedList (); 152 for (int i=0; i<60; i++) 153 { 154 ret.add(new Integer (i)); 155 } 156 return ret; 157 } 158 159 public List getSecondsList() 160 { 161 return getMinutesList(); 162 } 163 164 public boolean getHasPagePlugins() 165 { 166 return ContextLoader.hasPlugins(); 167 } 168 169 public String getEditorPage() 170 { 171 String editorPage = rollerRequest.getWebsite().getEditorPage(); 174 if (StringUtils.isEmpty( editorPage )) 175 { 176 editorPage = "editor-text.jsp"; 177 } 178 return editorPage; 179 } 180 181 public CalendarModel getCalendarModel() throws Exception 182 { 183 ActionForward selfForward = getMapping().findForward("editWeblog"); 185 String selfUrl= getRequest().getContextPath()+selfForward.getPath(); 186 187 CalendarModel model = new EditWeblogCalendarModel( 189 rollerRequest, getResponse(), selfUrl ); 190 return model; 191 } 192 193 public UserData getUser() 194 { 195 return rollerRequest.getUser(); 196 } 197 198 public List getCategories() throws Exception 199 { 200 return rollerRequest.getRoller().getWeblogManager() 201 .getWeblogCategories(rollerRequest.getWebsite(), false); 202 } 203 204 public List getComments() throws Exception 205 { 206 return comments; 207 } 208 209 public WeblogEntryFormEx getWeblogEntryForm() throws RollerException 210 { 211 return this.form; 212 } 213 214 215 public WeblogEntryData getWeblogEntry() throws RollerException 216 { 217 if (weblogEntry == null) 218 { 219 weblogEntry = new WeblogEntryData(); 220 weblogEntry.setWebsite(rollerRequest.getWebsite()); 221 form.copyTo(weblogEntry, 222 getRequest().getLocale(), getRequest().getParameterMap()); 223 weblogEntry.setWebsite(rollerRequest.getWebsite()); 224 } 225 return weblogEntry; 226 } 227 228 public String getPermaLink() throws RollerException 229 { 230 String context = RollerContext 231 .getRollerContext(rollerRequest.getRequest()) 232 .getAbsoluteContextUrl(rollerRequest.getRequest()); 233 return context + getWeblogEntry().getPermaLink(); 234 } 235 236 public String getSpellCheckHtml() throws RollerException 237 { 238 String text = getWeblogEntry().getText(); 239 String escapeText = StringUtils.replace( text, "<", "{" ); 240 escapeText = StringUtils.replace( escapeText, ">", "}" ); 241 StringBuffer newText = new StringBuffer (escapeText); 242 ArrayList events = (ArrayList ) 243 getRequest().getSession().getAttribute("spellCheckEvents"); 244 SpellCheckEvent event = null; 245 String word = null; 246 int start = -1; 247 int end = -1; 248 String select = null; 249 for(ListIterator it=events.listIterator(events.size()); it.hasPrevious();) 250 { 251 event = (SpellCheckEvent)it.previous(); 252 word = event.getInvalidWord(); 253 start = event.getWordContextPosition(); 254 end = start + word.length(); 255 select = makeSelect(word, event.getSuggestions()); 256 257 newText.replace( start, end, select ); 258 } 259 escapeText = StringUtils.replace( newText.toString(), "}", ">" ); 260 escapeText = StringUtils.replace( escapeText, "{", "<" ); 261 return escapeText; 262 } 263 264 public static String makeSelect(String word, List words) 265 { 266 StringBuffer buf = new StringBuffer ("<select name=\""); 267 buf.append("replacementWords\" style=\"font-size: 10px;\">"); 268 buf.append("<option selected=\"selected\" value=\"").append(word); 269 buf.append("\">").append(word).append("</option>"); 270 if (words == null || words.size() < 1) 271 { 272 buf.append("<option value=\"").append(word); 273 buf.append("\">No Suggestions</option>"); 274 } 275 else 276 { 277 for (Iterator it2=words.iterator(); it2.hasNext();) 278 { 279 word = it2.next().toString(); 280 buf.append("<option value=\"").append(word); 281 buf.append("\">").append(word).append("</option>"); 282 } 283 } 284 buf.append("</select>"); 285 return buf.toString(); 286 } 287 288 291 public PageMode getMode() { 292 return mode; 293 } 294 295 298 public void setMode(PageMode mode) { 299 this.mode = mode; 300 } 301 302 public boolean getEditMode() 303 { 304 return mode.equals(EDIT_MODE); 305 } 306 307 public boolean getSpellMode() 308 { 309 return mode.equals(SPELL_MODE); 310 } 311 312 public boolean getPreviewMode() 313 { 314 return mode.equals(PREVIEW_MODE); 315 } 316 317 320 public ArrayList getWords() { 321 return words; 322 } 323 326 public void setWords(ArrayList words) { 327 this.words = words; 328 } 329 } 330 | Popular Tags |