* are taken from serviceInfo object. nameToServiceInfoHash
* contains a hash with id as a key and ServiceInfo as a value.
*/
public void addNameToServiceInfoHash(final ServiceInfo serviceInfo) {
/* add to the hash with service name and id as keys */
final Service service = serviceInfo.getService();
lockNameToServiceInfo();
Map<String, ServiceInfo> idToInfoHash = nameToServiceInfoHash.get(service.getName());
String csPmId = null;
final ServiceInfo cs = serviceInfo.getContainedService();
if (cs != null) {
csPmId = cs.getService().getName() + '_' + cs.getService().getId();
}
if (idToInfoHash == null) {
idToInfoHash = new TreeMap<String, ServiceInfo>(String.CASE_INSENSITIVE_ORDER);
if (service.getId() == null) {
if (csPmId == null) {
service.setId("1");
} else {
service.setIdAndCrmId(csPmId);
}
}
} else {
if (service.getId() == null) {
int index = 0;
for (final String id : idToInfoHash.keySet()) {
final Pattern p;
if (csPmId == null) {
p = Pattern.compile("^(\\d+)$");
} else {
/* ms */
p = Pattern.compile('^' + csPmId + "_(\\d+)$");
if (csPmId.equals(id)) {
index++;
}
}
final Matcher m = p.matcher(id);
if (m.matches()) {
try {
final int i = Integer.parseInt(m.group(1));
if (i > index) {
index = i;
}
} catch (final NumberFormatException nfe) {
LOG.appWarning("addNameToServiceInfoHash: could not parse: " + m.group(1));
}
}
}
if (csPmId == null) {
service.setId(Integer.toString(index + 1));
} else {
/* ms */
if (index == 0) {
service.setIdAndCrmId(csPmId);
} else {
service.setIdAndCrmId(csPmId + '_' + Integer.toString(index + 1));
}
}
}
}
idToInfoHash.put(service.getId(), serviceInfo);
nameToServiceInfoHash.put(service.getName(), idToInfoHash);
unlockNameToServiceInfo();
}