1 23 package org.apache.slide.macro; 24 25 import org.apache.slide.common.Domain; 26 import org.apache.slide.common.Uri; 27 import org.apache.slide.content.Content; 28 import org.apache.slide.content.ContentImpl; 29 import org.apache.slide.content.NodeRevisionDescriptor; 30 import org.apache.slide.content.NodeRevisionDescriptors; 31 import org.apache.slide.event.MacroEvent; 32 import org.apache.slide.event.MacroListener; 33 import org.apache.slide.event.VetoException; 34 import org.apache.slide.lock.Lock; 35 import org.apache.slide.lock.LockImpl; 36 import org.apache.slide.security.ACLSecurityImpl; 37 import org.apache.slide.security.Security; 38 import org.apache.slide.structure.Structure; 39 import org.apache.slide.structure.StructureImpl; 40 import org.apache.slide.structure.SubjectNode; 41 import org.apache.slide.util.conf.Configurable; 42 import org.apache.slide.util.conf.Configuration; 43 import org.apache.slide.util.conf.ConfigurationException; 44 45 52 public class MacroPropertyUpdater 53 implements MacroListener, Configurable 54 { 55 private boolean updateDisplayName = true; 56 private boolean updateOwnerOnMove = false; 57 private boolean updateOwnerOnCopy = true; 58 59 62 public void configure(Configuration configuration) 63 throws ConfigurationException 64 { 65 66 try { 67 this.updateDisplayName = configuration.getConfiguration("update-displayname") 68 .getValueAsBoolean(this.updateDisplayName); 69 } catch (Exception e) { 70 } 72 73 try { 74 this.updateOwnerOnMove = configuration.getConfiguration("update-owner-on-move") 75 .getValueAsBoolean(this.updateOwnerOnMove); 76 } catch (Exception e) { 77 } 79 80 try { 81 this.updateOwnerOnCopy = configuration.getConfiguration("update-owner-on-copy") 82 .getValueAsBoolean(this.updateOwnerOnCopy); 83 } catch (Exception e) { 84 } 86 } 87 88 91 public void copy(MacroEvent event) throws VetoException 92 { 93 try { 94 Content helper = getContentHelper(event); 95 96 NodeRevisionDescriptors destNrds = helper.retrieve(event.getToken(), 97 event.getTargetURI()); 98 NodeRevisionDescriptor destNrd = helper.retrieve(event.getToken(), 99 destNrds); 100 101 boolean anyThingUpdated = false; 102 103 if (this.updateDisplayName) { 105 String displayName = getFilename(event.getTargetURI()); 106 if (!displayName.equals(destNrd.getName())) { 107 destNrd.setName(displayName); 108 anyThingUpdated = true; 109 } 110 } 111 112 if (this.updateOwnerOnCopy) { 114 String newOwner = event.getToken().getCredentialsToken().getPublicCredentials(); 115 if (newOwner == null || newOwner.equals("") || newOwner.equals("/")) { 116 newOwner = SubjectNode.UNAUTHENTICATED_URI; 117 } 118 if (!destNrd.getOwner().equals(newOwner)) { 119 destNrd.setOwner(newOwner); 120 anyThingUpdated = true; 121 } 122 } 123 124 if (anyThingUpdated) { 125 Uri target = event.getNamespace().getUri(event.getToken(), 126 event.getTargetURI()); 127 target.getStore().storeRevisionDescriptor(target, destNrd); 128 } 129 130 } 131 catch (Exception e) { 132 Domain.error("Exception while copy", e); 133 } 134 135 } 136 137 140 public void move(MacroEvent event) throws VetoException 141 { 142 try { 143 Content helper = getContentHelper(event); 144 145 NodeRevisionDescriptors destNrds = helper.retrieve(event.getToken(), 146 event.getTargetURI()); 147 NodeRevisionDescriptor destNrd = helper.retrieve(event.getToken(), 148 destNrds); 149 150 boolean anyThingUpdated = false; 151 152 if (this.updateOwnerOnMove) { 154 String newOwner = event.getToken().getCredentialsToken().getPublicCredentials(); 155 if (newOwner == null || newOwner.equals("") || newOwner.equals("/")) { 156 newOwner = SubjectNode.UNAUTHENTICATED_URI; 157 } 158 if (!destNrd.getOwner().equals(newOwner)) { 159 anyThingUpdated = true; 160 destNrd.setOwner(newOwner); 161 } 162 } 163 164 if (anyThingUpdated) { 165 Uri target = event.getNamespace().getUri(event.getToken(), event.getTargetURI()); 166 target.getStore().storeRevisionDescriptor(target, destNrd); 167 } 168 } 169 catch (Exception e) { 170 Domain.error("Exception while move", e); 171 } 172 } 173 174 175 178 public void delete(MacroEvent event) throws VetoException 179 { 180 } 181 182 183 190 private static String getFilename(String uri) { 191 if (uri.indexOf('/') == -1) { 192 return uri; 193 } 194 if (uri.equals("/")) { 195 return ""; 196 } 197 if (uri.endsWith("/")) { 198 return uri.substring(1+uri.lastIndexOf('/', uri.length() - 2), 199 uri.length() - 1); 200 } else { 201 return uri.substring(1+uri.lastIndexOf('/')); 202 } 203 } 204 205 private Content getContentHelper(MacroEvent event) { 206 Security security = new ACLSecurityImpl(event.getNamespace(), 207 event.getNamespace().getConfig()); 208 Lock lock = new LockImpl(event.getNamespace(), 209 event.getNamespace().getConfig(), 210 security); 211 Structure structure = new StructureImpl(event.getNamespace(), 212 event.getNamespace().getConfig(), 213 security, 214 lock); 215 Content helper = new ContentImpl( 216 event.getNamespace(), 217 event.getNamespace().getConfig(), 218 security, 219 structure, 220 lock); 221 return helper; 222 } 223 } 224 | Popular Tags |