1 16 package org.outerj.daisy.repository.commonimpl.variant; 17 18 import org.outerj.daisy.repository.variant.Branch; 19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 20 import org.outerj.daisy.repository.commonimpl.Util; 21 import org.outerj.daisy.repository.RepositoryException; 22 import org.outerx.daisy.x10.BranchDocument; 23 24 import java.util.Date ; 25 import java.util.GregorianCalendar ; 26 27 public class BranchImpl implements Branch { 28 private long id = -1; 29 private String name; 30 private String description; 31 private boolean readOnly = false; 32 private long lastModifier; 33 private Date lastModified; 34 private long updateCount = 0; 35 private AuthenticatedUser currentUser; 36 private VariantStrategy strategy; 37 private IntimateAccess intimateAccess = new IntimateAccess(); 38 private static final String READ_ONLY_MESSAGE = "This Branch object is read-only."; 39 40 public BranchImpl(VariantStrategy strategy, String name, AuthenticatedUser currentUser) { 41 Util.checkName(name); 42 this.strategy = strategy; 43 this.name = name; 44 this.currentUser = currentUser; 45 } 46 47 public long getId() { 48 return id; 49 } 50 51 public String getName() { 52 return name; 53 } 54 55 public String getDescription() { 56 return description; 57 } 58 59 public void setName(String name) { 60 if (readOnly) 61 throw new RuntimeException (READ_ONLY_MESSAGE); 62 63 Util.checkName(name); 64 this.name = name; 65 } 66 67 public void setDescription(String description) { 68 if (readOnly) 69 throw new RuntimeException (READ_ONLY_MESSAGE); 70 71 this.description = description; 72 } 73 74 public void save() throws RepositoryException { 75 strategy.storeBranch(this); 76 } 77 78 public long getLastModifier() { 79 return lastModifier; 80 } 81 82 public Date getLastModified() { 83 return lastModified; 84 } 85 86 public long getUpdateCount() { 87 return updateCount; 88 } 89 90 public BranchDocument getXml() { 91 BranchDocument branchDocument = BranchDocument.Factory.newInstance(); 92 BranchDocument.Branch branch = branchDocument.addNewBranch(); 93 94 branch.setName(name); 95 if (description != null) 96 branch.setDescription(description); 97 98 if (id != -1) { 99 branch.setId(id); 100 101 GregorianCalendar lastModifiedCalendar = new GregorianCalendar (); 102 lastModifiedCalendar.setTime(lastModified); 103 branch.setLastModified(lastModifiedCalendar); 104 105 branch.setLastModifier(lastModifier); 106 branch.setUpdateCount(updateCount); 107 } 108 109 return branchDocument; 110 } 111 112 public void setAllFromXml(BranchDocument.Branch branchXml) { 113 if (readOnly) 114 throw new RuntimeException (READ_ONLY_MESSAGE); 115 116 setName(branchXml.getName()); 117 setDescription(branchXml.getDescription()); 118 } 119 120 public void makeReadOnly() { 121 this.readOnly = true; 122 } 123 124 public IntimateAccess getIntimateAccess(VariantStrategy strategy) { 125 if (readOnly) 126 throw new RuntimeException (READ_ONLY_MESSAGE); 127 128 if (this.strategy == strategy) 129 return intimateAccess; 130 else 131 return null; 132 } 133 134 public class IntimateAccess { 135 private IntimateAccess() { 136 } 137 138 public AuthenticatedUser getCurrentUser() { 139 return currentUser; 140 } 141 142 public void setId(long id) { 143 BranchImpl.this.id = id; 144 } 145 146 public void setLastModified(Date lastModified) { 147 BranchImpl.this.lastModified = lastModified; 148 } 149 150 public void setLastModifier(long lastModifier) { 151 BranchImpl.this.lastModifier = lastModifier; 152 } 153 154 public void setUpdateCount(long updateCount) { 155 BranchImpl.this.updateCount = updateCount; 156 } 157 } 158 } 159 | Popular Tags |