1 package com.dotmarketing.portlets.structure.action; 2 3 import java.net.URLDecoder ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.regex.Matcher ; 7 import java.util.regex.Pattern ; 8 9 import javax.portlet.ActionRequest; 10 import javax.portlet.ActionResponse; 11 import javax.portlet.PortletConfig; 12 import javax.servlet.http.HttpServletRequest ; 13 14 import org.apache.commons.beanutils.BeanUtils; 15 import org.apache.struts.action.ActionForm; 16 import org.apache.struts.action.ActionMapping; 17 18 import com.dotmarketing.beans.Identifier; 19 import com.dotmarketing.cache.StructureCache; 20 import com.dotmarketing.db.DotHibernate; 21 import com.dotmarketing.factories.InodeFactory; 22 import com.dotmarketing.portal.struts.DotPortletAction; 23 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 24 import com.dotmarketing.portlets.contentlet.model.Contentlet; 25 import com.dotmarketing.portlets.entities.model.Entity; 26 import com.dotmarketing.portlets.structure.factories.FieldFactory; 27 import com.dotmarketing.portlets.structure.factories.StructureFactory; 28 import com.dotmarketing.portlets.structure.model.Field; 29 import com.dotmarketing.portlets.structure.model.Structure; 30 import com.dotmarketing.portlets.structure.struts.StructureForm; 31 import com.dotmarketing.util.Config; 32 import com.dotmarketing.util.Logger; 33 import com.dotmarketing.util.UtilMethods; 34 import com.dotmarketing.util.Validator; 35 import com.dotmarketing.util.WebKeys; 36 import com.liferay.portal.model.User; 37 import com.liferay.portal.struts.ActionException; 38 import com.liferay.portal.util.Constants; 39 import com.liferay.portlet.ActionRequestImpl; 40 import com.liferay.util.servlet.SessionMessages; 41 42 public class EditStructureAction extends DotPortletAction { 43 44 public void processAction(ActionMapping mapping, ActionForm form, 45 PortletConfig config, ActionRequest req, ActionResponse res) 46 throws Exception { 47 48 String cmd = req.getParameter(Constants.CMD); 49 String referer = req.getParameter("referer"); 50 51 if ((referer != null) && (referer.length() != 0)) { 52 referer = URLDecoder.decode(referer, "UTF-8"); 53 } 54 55 try { 57 Logger.debug(this, "Calling Retrieve method"); 58 _loadStructure(form, req, res); 59 } catch (Exception ae) { 60 _handleException(ae, req); 61 return; 62 } 63 64 DotHibernate.startTransaction(); 65 66 71 if ((cmd != null) && cmd.equals(Constants.ADD)) { 72 try { 73 Logger.debug(this, "Calling Add/Edit Method"); 74 if (Validator.validate(req, form, mapping)) { 75 _saveStructure(form, req, res); 76 } 77 } 78 79 catch (Exception ae) { 80 _handleException(ae, req); 81 return; 82 } 83 84 } 85 90 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 91 try { 92 Logger.debug(this, "Calling Delete Method"); 93 _deleteStructure(form, req, res); 94 } catch (Exception ae) { 95 _handleException(ae, req); 96 return; 97 } 98 _sendToReferral(req, res, referer); 99 return; 100 } else if ((cmd != null) && cmd.equals(WebKeys.Structure.SET_DEFAULT)) { 101 try { 102 _defaultStructure(form, req, res); 103 } catch (Exception ae) { 104 _handleException(ae, req); 105 return; 106 } 107 _sendToReferral(req, res, referer); 108 return; 109 } else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.RESET)) { 110 try { 111 _resetIntervals(form, req, res); 112 } catch (Exception ae) { 113 _handleException(ae, req); 114 return; 115 } 116 _sendToReferral(req, res, referer); 117 return; 118 } 119 120 DotHibernate.commitTransaction(); 121 _loadForm(form, req, res); 122 setForward(req, "portlet.ext.structure.edit_structure"); 123 } 124 125 private Structure _loadStructure(ActionForm form, ActionRequest req, 126 ActionResponse res) throws ActionException { 127 128 User user = _getUser(req); 129 Structure structure = new Structure(); 130 String inodeString = req.getParameter("inode"); 131 if (UtilMethods.isSet(inodeString)) { 132 long inode = Long.parseLong(inodeString); 133 if (inode != 0) { 134 structure = StructureFactory.getStructureByInode(inode); 135 } 136 } 137 req.setAttribute(WebKeys.Structure.STRUCTURE, structure); 138 139 req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, structure); 141 req.setAttribute(WebKeys.PERMISSION_SHOW_APPLY_TO_SUBFOLDERS, "true"); 142 143 boolean searchable = false; 144 145 List <Field> fields = structure.getFields(); 146 for (Field f : fields) { 147 if (f.isIndexed()) { 148 searchable = true; 149 break; 150 } 151 } 152 153 if (!searchable && structure.getInode() != 0) { 154 String message = "warning.structure.notsearchable"; 155 SessionMessages.add(req, "message", message); 156 157 } 158 159 _checkUserPermissions(structure, user, Config 161 .getIntProperty("PERMISSION_READ")); 162 163 return structure; 164 } 165 166 private void _saveStructure(ActionForm form, ActionRequest req, 167 ActionResponse res) { 168 try { 169 boolean newStructure = false; 170 StructureForm structureForm = (StructureForm) form; 171 Structure structure = (Structure) req 172 .getAttribute(WebKeys.Structure.STRUCTURE); 173 174 User user = _getUser(req); 175 HttpServletRequest httpReq = ((ActionRequestImpl) req) 176 .getHttpServletRequest(); 177 178 _checkWritePermissions(structure, user, httpReq); 180 181 if (structure.getInode() == 0) { 183 newStructure = true; 184 } else { 185 String structureName = structure.getName(); 186 String structureFormName = structureForm.getName(); 187 if (UtilMethods.isSet(structureName) 188 && UtilMethods.isSet(structureFormName) 189 && !structureName.equals(structureFormName)) { 190 Entity entity = (Entity) InodeFactory 191 .getParentOfClassByRelationType(structure, 192 Entity.class, 193 WebKeys.Structure.STRUCTURE_ENTITY); 194 if (entity.getInode() != 0) { 195 entity.setEntityName(structureFormName); 196 InodeFactory.saveInode(entity); 197 } 198 } 199 } 200 201 BeanUtils.copyProperties(structure, structureForm); 202 203 if (structureForm.isReviewContent()) { 205 structure.setReviewInterval(structureForm.getReviewIntervalNum() + structureForm.getReviewIntervalSelect()); 206 } else { 207 structure.setReviewInterval(null); 208 } 209 210 Structure defaultStructure = StructureFactory.getDefaultStructure(); 212 if (defaultStructure.getInode() == 0) { 213 structure.setDefaultStructure(true); 214 } 215 StructureFactory.saveStructure(structure); 216 if (newStructure) { 218 Entity entity = new Entity(); 219 entity.setEntityName(structureForm.getName()); 220 InodeFactory.saveInode(entity); 221 structure.addParent(entity, WebKeys.Structure.STRUCTURE_ENTITY); 222 } 223 224 _applyPermissions(req, WebKeys.Structure.STRUCTURE); 225 226 StructureCache.addStructure(structure); 228 229 String message = "message.structure.savestructure"; 230 SessionMessages.add(req, "message", message); 231 } catch (Exception ex) { 232 Logger.debug(EditStructureAction.class, ex.toString()); 233 } 234 } 235 236 private void _resetIntervals(ActionForm form, ActionRequest req, 237 ActionResponse res) { 238 try { 239 Structure structure = (Structure) req.getAttribute(WebKeys.Structure.STRUCTURE); 240 241 List <Contentlet> contents = ContentletFactory.getContentletsByStructure(structure.getInode()); 242 243 for (Contentlet cont : contents) { 244 cont.setReviewInterval(structure.getReviewInterval()); 245 } 246 } catch (Exception ex) { 247 Logger.debug(EditStructureAction.class, ex.toString()); 248 } 249 } 250 251 private void _loadForm(ActionForm form, ActionRequest req, 252 ActionResponse res) { 253 try { 254 StructureForm structureForm = (StructureForm) form; 255 Structure structure = (Structure) req 256 .getAttribute(WebKeys.Structure.STRUCTURE); 257 BeanUtils.copyProperties(structureForm, structure); 258 structureForm.setFields(structure.getFields()); 259 260 if (structure.getReviewInterval() != null) { 261 String interval = structure.getReviewInterval(); 262 Pattern p = Pattern.compile("(\\d+)([dmy])"); 263 Matcher m = p.matcher(interval); 264 boolean b = m.matches(); 265 if (b) { 266 structureForm.setReviewContent(true); 267 String g1 = m.group(1); 268 String g2 = m.group(2); 269 structureForm.setReviewIntervalNum(g1); 270 structureForm.setReviewIntervalSelect(g2); 271 } 272 } 273 274 } catch (Exception ex) { 275 Logger.debug(EditStructureAction.class, ex.toString()); 276 } 277 } 278 279 private void _deleteStructure(ActionForm form, ActionRequest req, 280 ActionResponse res) throws Exception { 281 282 try { 283 Structure structure = (Structure) req 284 .getAttribute(WebKeys.Structure.STRUCTURE); 285 286 User user = _getUser(req); 287 HttpServletRequest httpReq = ((ActionRequestImpl) req) 288 .getHttpServletRequest(); 289 290 _checkDeletePermissions(structure, user, httpReq); 292 293 if (!structure.isDefaultStructure()) { 294 List fields = FieldFactory.getFieldByStructureType(structure 295 .getInode()); 296 Iterator fieldsIter = fields.iterator(); 297 while (fieldsIter.hasNext()) { 298 Field field = (Field) fieldsIter.next(); 299 FieldFactory.deleteField(field); 300 } 301 List contentlets = ContentletFactory.getContentletsByStructure(structure.getInode()); 303 Iterator contentletsIter = contentlets.iterator(); 304 while (contentletsIter.hasNext()) { 305 Contentlet contentlet = (Contentlet) contentletsIter.next(); 306 Identifier id = new Identifier(); 307 id.setInode(contentlet.getIdentifier()); 308 if(id.getInode()>0){ 309 InodeFactory.deleteInode(id); 310 } 311 InodeFactory.deleteInode(contentlet); 312 } 313 314 StructureFactory.deleteStructure(structure); 315 316 StructureCache.removeStructure(structure); 318 319 SessionMessages.add(req, "message", 320 "message.structure.deletestructure"); 321 } else { 322 SessionMessages.add(req, "message", 323 "message.structure.notdeletestructure"); 324 } 325 } catch (Exception ex) { 326 Logger.debug(EditStructureAction.class, ex.toString()); 327 } 328 } 329 330 private void _defaultStructure(ActionForm form, ActionRequest req, 331 ActionResponse res) { 332 try { 333 334 Structure structure = (Structure) req 335 .getAttribute(WebKeys.Structure.STRUCTURE); 336 337 User user = _getUser(req); 338 HttpServletRequest httpReq = ((ActionRequestImpl) req) 339 .getHttpServletRequest(); 340 _checkWritePermissions(structure, user, httpReq); 341 342 StructureFactory.disableDefault(); 343 structure.setDefaultStructure(true); 344 StructureFactory.saveStructure(structure); 345 String message = "message.structure.defaultstructure"; 346 SessionMessages.add(req, "message", message); 347 } catch (Exception ex) { 348 Logger.debug(EditStructureAction.class, ex.toString()); 349 } 350 351 } 352 } 353 | Popular Tags |