Working Code:
@SuppressWarnings("unchecked")
public void addEnumerationLiteral (IAttribute attribute, String identifierName, String identifierCode)
throws TeamRepositoryException {
Enumeration enumeration= (Enumeration) enumerationService.resolveEnumeration(attribute);
if (enumeration != null) {
Enumeration enumCopy= (Enumeration) enumeration.getWorkingCopy();
Literal literal = EnumerationFactory.eINSTANCE.createLiteral();
com.ibm.team.workitem.common.internal.util.Utils.initNew(literal);
IEnumeration<?> iEnumeration = this.workItemCommon.resolveEnumeration((IAttributeHandle) attribute.getItemHandle(), this.monitor);
if (identifierCode==null) {
identifierCode = getNextEnumerationIdentifierString(iEnumeration);
}
literal.setIdentifier(identifierCode);
literal.setName(identifierName);
literal.setSequenceValue(((Enumeration) enumeration).getLiterals().size()-1);
((Enumeration) enumCopy).getLiterals().add(literal);
if (((Enumeration) enumCopy).getLiterals().size() == 1) {
((Enumeration) enumCopy).setDefaultLiteralId(literal.getIdentifier());
}
// asynchronous operation?
enumerationService.update((Enumeration) enumCopy);
}
}
public ILiteral getLastEnumerationLiteral (IEnumeration<?> enumeration) {
ILiteral lastLiteral;
List<?> literals = enumeration.getEnumerationLiterals(true);
List<ILiteral> orderedLiterals = new ArrayList<ILiteral>();
for (Iterator<?> literalsIterator = literals.iterator(); literalsIterator.hasNext();) {
// default: last position
int orderedPosition = orderedLiterals.size();
ILiteral literal = (ILiteral) literalsIterator.next();
Hashtable<String, String> currentLiteralInfo = this.getLiteralStringInfo(literal);
String currentLiteralNumberString = currentLiteralInfo.get("number");
Integer currentLiteralNumber = null;
if (currentLiteralNumberString!=null) {
currentLiteralNumber = Integer.parseInt(currentLiteralNumberString);
}
if (currentLiteralNumber!=null) {
for (int i = 0; i<orderedLiterals.size(); i++) {
Hashtable<String, String> orderedLiteralInfo = this.getLiteralStringInfo(orderedLiterals.get(i));
String orderedLiteralNumberString = orderedLiteralInfo.get("number");
Integer orderedLiteralNumber = null;
if (orderedLiteralNumberString!=null) {
orderedLiteralNumber = Integer.parseInt(orderedLiteralNumberString);
}
if (orderedLiteralNumber!=null) {
if (currentLiteralNumber<orderedLiteralNumber) {
orderedPosition = i;
break;
}
}
}
orderedLiterals.add(orderedPosition, literal);
}
}
lastLiteral = orderedLiterals.get(orderedLiterals.size()-1);
return lastLiteral;
}
public String getNextEnumerationIdentifierString (IEnumeration<?> enumeration) {
// avoid using when adding multiple identifiers all at once
String nextIdentifierString = "";
ILiteral lastLiteral = this.getLastEnumerationLiteral(enumeration);
Hashtable<String, String> lastLiteralInfo = this.getLiteralStringInfo(lastLiteral);
String prefix = "";
String letter = "";
String numberString = null;
prefix = lastLiteralInfo.get("prefix");
letter = lastLiteralInfo.get("letter");
numberString = lastLiteralInfo.get("number");
if ((!prefix.isEmpty()) && (!letter.isEmpty()) && (!numberString.isEmpty())) {
Integer nextNumber = Integer.parseInt(numberString) + 2;
nextIdentifierString = prefix + "." + letter + nextNumber.toString();
}
return nextIdentifierString;
}
public Hashtable<String, String> getLiteralStringInfo (ILiteral literal) {
Hashtable<String, String> literalStringInfo = new Hashtable<String, String>();
String prefix = "";
String letter = "";
Integer number = null;
// ibm.enumeration.waivertype.literal.l2
String literalRegularExpression = "^(.+)\.(\D+)(\d+)$";
Pattern literalPattern = Pattern.compile(literalRegularExpression);
Matcher literalMatcher = literalPattern.matcher(literal.getIdentifier2().getStringIdentifier());
while (literalMatcher.find()) {
prefix = literalMatcher.group(1);
letter = literalMatcher.group(2);
number = Integer.parseInt(literalMatcher.group(3));
literalStringInfo.put("prefix", prefix);
literalStringInfo.put("letter", letter);
literalStringInfo.put("number", number.toString());
}
return literalStringInfo;
}