1 package com.dotmarketing.threads; 2 3 import java.util.Date ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 7 import com.dotmarketing.beans.Host; 8 import com.dotmarketing.beans.Identifier; 9 import com.dotmarketing.db.DotHibernate; 10 import com.dotmarketing.factories.HostFactory; 11 import com.dotmarketing.factories.IdentifierFactory; 12 import com.dotmarketing.factories.InodeFactory; 13 import com.dotmarketing.factories.RoleFactory; 14 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 15 import com.dotmarketing.portlets.contentlet.model.Contentlet; 16 import com.dotmarketing.portlets.structure.model.Structure; 17 import com.dotmarketing.portlets.workflows.factories.WorkflowsFactory; 18 import com.dotmarketing.portlets.workflows.model.WorkflowHistory; 19 import com.dotmarketing.portlets.workflows.model.WorkflowTask; 20 import com.dotmarketing.util.Config; 21 import com.dotmarketing.util.Logger; 22 import com.dotmarketing.util.UtilMethods; 23 import com.dotmarketing.util.WebKeys.WorkflowStatuses; 24 import com.liferay.portal.NoSuchRoleException; 25 26 public class ContentReviewThread implements Runnable { 27 28 public ContentReviewThread() { 29 } 30 31 public void run() { 32 try { 33 Logger.debug(this, "Starting ContentsReview"); 34 DotHibernate.startTransaction(); 35 36 DotHibernate dh = new DotHibernate(Contentlet.class); 37 dh.setSQLQuery("select {contentlet.*} from contentlet, inode contentlet_1_, structure " 38 + "where contentlet.inode = contentlet_1_.inode and " 39 + "? >= contentlet.next_review and " 40 + "contentlet.review_interval is not null and contentlet.review_interval <> '' and " 41 + "contentlet.structure_inode = structure.inode and " 42 + "structure.reviewer_role is not null and structure.reviewer_role <> '' and " 43 + "contentlet.working = ? "); 44 dh.setParam(new Date ()); 45 dh.setParam(true); 46 List <Contentlet> contentlets = dh.list(); 47 HashMap <Long , Structure> structures = new HashMap <Long , Structure>(); 48 String systemUserId = "System"; 49 for (Contentlet cont : contentlets) { 50 try { 51 52 cont.setNextReview(ContentletFactory.calculateNextReviewDate(cont.getReviewInterval())); 53 Structure st = structures.get(cont.getStructureInode()); 54 if (st == null) { 55 st = (Structure) InodeFactory.getInode(cont.getStructureInode(), Structure.class); 56 structures.put(st.getInode(), st); 57 } 58 59 WorkflowTask task = new WorkflowTask(); 60 61 Identifier id = IdentifierFactory.getIdentifierByInode(cont); 62 task.setTitle("Content: " + id.getInode() + " needs to be reviewed."); 63 64 task.setDescription(cont.toPrettyHTMLString()); 65 66 task.setBelongsTo(st.getReviewerRole()); 67 task.setModDate(new Date ()); 68 task.setCreationDate(new Date ()); 69 task.setCreatedBy(systemUserId); 70 task.setAssignedTo("Nobody"); 71 task.setDueDate(null); 72 task.setWebasset(cont.getInode()); 73 task.setStatus(WorkflowStatuses.OPEN.toString()); 74 75 InodeFactory.saveInode(task); 76 77 WorkflowHistory hist = new WorkflowHistory(); 78 hist.setChangeDescription("Review Content Task Creation"); 79 hist.setCreationDate(new Date ()); 80 hist.setMadeBy(systemUserId); 81 InodeFactory.saveInode(hist); 82 task.addChild(hist); 83 84 String body = _buildWorkflowEmailBody (task, "New Review Task"); 85 WorkflowsFactory.sendWorkflowChangeEmails(task, "A new Review Content Task was created", body); 86 87 } catch (Exception e) { 88 Logger.error(this, "Error ocurred trying to create the review task for contenlet: " 89 + cont.getInode(), e); 90 } 91 } 92 93 } catch (Exception e) { 94 Logger.error(this, "Error ocurred trying to review contents.", e); 95 } finally { 96 DotHibernate.commitTransaction(); 97 } 98 } 99 100 private String _buildWorkflowEmailBody (WorkflowTask task, String change) { 101 102 Host host = HostFactory.getDefaultHost(); 103 String ref = "http://" + host.getHostname() + Config.getStringProperty("WORKFLOWS_URL") + "&inode=" + String.valueOf(task.getInode()); 104 105 StringBuffer buffer = new StringBuffer (); 106 107 try { 108 String roleName = RoleFactory.getRoleById(task.getBelongsTo()).getName(); 109 110 buffer.append( 111 "<table align=\"center\" border=\"1\" width=\"50%\">" + 112 " <tr>" + 113 " <td align=\"center\" colspan=\"2\"><b>" + change + "</b></td>" + 114 " </tr>" + 115 " <tr>" + 116 " <td width=\"15%\" nowrap><b>Task</b></td><td><a HREF=\"" + ref + "\">" + task.getTitle() + "</a></td>" + 117 " </tr>" + 118 " <tr>" + 119 " <td nowrap><b>Created</b></td><td>" + UtilMethods.dateToHTMLDate(task.getCreationDate()) + "</td>" + 120 " </tr>" + 121 " <tr>" + 122 " <td nowrap><b>Author</b></td><td>" + UtilMethods.getUserFullName(task.getCreatedBy()) + "</td>" + 123 " </tr>" + 124 " <tr>" + 125 " <td nowrap><b>Asignee Group</b></td><td>" + roleName + "</td>" + 126 " </tr>" + 127 "</table>" 128 ); 129 } catch (NoSuchRoleException e) { 130 Logger.warn(RoleFactory.class, "_buildWorkflowEmailBody: Error getting role", e); 131 } 132 133 return buffer.toString(); 134 } 135 136 141 public void destroy() { 142 DotHibernate.closeSession(); 143 } 144 } 145 | Popular Tags |