001 /*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License"). You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at
010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 * Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 * Copyright 2006-2008 Sun Microsystems, Inc.
026 */
027 package org.opends.server.replication.plugin;
028
029 import java.util.LinkedHashSet;
030 import java.util.Set;
031
032 import org.opends.server.core.DirectoryServer;
033 import org.opends.server.replication.common.ChangeNumber;
034 import org.opends.server.types.Attribute;
035 import org.opends.server.types.AttributeType;
036 import org.opends.server.types.AttributeValue;
037 import org.opends.server.types.Modification;
038 import org.opends.server.types.ModificationType;
039
040
041 /**
042 * This Class is used to encode/decode historical information
043 * from the String form to the internal usable form.
044 *
045 * @author Gilles Bellaton
046 */
047 public class HistVal
048 {
049 private AttributeType attrType;
050 private String attrString;
051 private AttributeValue attributeValue;
052 private ChangeNumber cn;
053 private LinkedHashSet<String> options;
054 private HistKey histKey;
055 private String stringValue;
056
057 /**
058 * Create a new HistVal form the String encoded form.
059 *
060 * @param strVal The String encoded form of historical information.
061 */
062 public HistVal(String strVal)
063 {
064 /*
065 * an historical attribute value looks like :
066 * description:00000108b3a6554100000001:add:added_value
067 * or
068 * description:00000108b3a6cbb800000001:del:deleted_value
069 * or
070 * description:00000108b3a6cbb800000001:repl:new_value
071 * or
072 * description:00000108b3a6cbb800000001:delAttr
073 * or
074 * description:00000108b3a6554100000001:add
075 * or
076 *
077 * so after split
078 * token[0] will contain the attribute name
079 * token[1] will contain the changenumber
080 * token[2] will contain the type of historical information
081 * token[3] will contain the attribute value
082 *
083 * options are stored with the attribute names using; as a separator
084 * example :
085 * description;FR;France:00000108b3a6554100000001:add:added_value
086 */
087 String[] token = strVal.split(":", 4);
088
089 options = new LinkedHashSet<String>();
090 if (token[0].contains(";"))
091 {
092 String[] optionsToken = token[0].split(";");
093 int index = 1;
094 while (index < optionsToken.length)
095 {
096 options.add(optionsToken[index]);
097 index ++;
098 }
099 attrString = optionsToken[0];
100 }
101 else
102 {
103 attrString = token[0];
104 }
105
106 attrType = DirectoryServer.getSchema().getAttributeType(attrString);
107 if (attrType == null)
108 attrType = DirectoryServer.getDefaultAttributeType(attrString);
109
110 cn = new ChangeNumber(token[1]);
111 histKey = HistKey.decodeKey(token[2]);
112 stringValue = null;
113 if (histKey != HistKey.DELATTR)
114 {
115 if (token.length == 4)
116 {
117 stringValue = token[3];
118 attributeValue = new AttributeValue(attrType, stringValue);
119 }
120 else
121 attributeValue = null;
122 }
123 else
124 {
125 stringValue = null;
126 attributeValue = null;
127 }
128 }
129
130 /**
131 * Get the String form of the attribute type.
132 *
133 * @return Returns the String form of the attribute type.
134 */
135 public String getAttrString()
136 {
137 return attrString;
138 }
139
140 /**
141 * Get the type of this HistVal.
142 * @return Returns the type of this HistVal.
143 */
144 public AttributeType getAttrType()
145 {
146 return attrType;
147 }
148
149 /**
150 * Get the ChangeNUmber of this HistVal.
151 * @return Returns the ChangeNumber of this HistVal.
152 */
153 public ChangeNumber getCn()
154 {
155 return cn;
156 }
157
158 /**
159 * Get the HistKey.
160 * @return Returns the histKey.
161 */
162 public HistKey getHistKey()
163 {
164 return histKey;
165 }
166
167 /**
168 * Get the options or an empty set if there are no options.
169 * @return Returns the options.
170 */
171 public Set<String> getOptions()
172 {
173 return options;
174 }
175
176 /**
177 * Get the String form of the attribute.
178 * @return The String form of the attribute.
179 */
180 public String getStringValue()
181 {
182 return stringValue;
183 }
184
185 /**
186 * Get the Attribute Value.
187 * @return The Attribute Value.
188 */
189 public AttributeValue getAttributeValue()
190 {
191 return attributeValue;
192 }
193
194 /**
195 * Generate a Modification equivalent to this HistVal.
196 *
197 * @return A Modification equivalent to this HistVal.
198 */
199 public Modification generateMod()
200 {
201 Attribute attr = new Attribute(attrType, attrString, options, null);
202 Modification mod;
203 if (histKey != HistKey.DELATTR)
204 {
205 LinkedHashSet<AttributeValue> values =
206 new LinkedHashSet<AttributeValue>(1);
207 values.add(attributeValue);
208 attr.setValues(values);
209 }
210 switch (histKey)
211 {
212 case ADD : mod = new Modification(ModificationType.ADD, attr);
213 break;
214 case DEL : mod = new Modification(ModificationType.DELETE, attr);
215 break;
216 case REPL: mod = new Modification(ModificationType.REPLACE, attr);
217 break;
218 case DELATTR: mod = new Modification(ModificationType.DELETE, attr);
219 break;
220 default: mod = null;
221 }
222 return mod;
223 }
224 }