* @throws ClipsServerException
*/
@Override
protected void set(AddressObject entity, AddressObjectDetails details) throws ClipsServerException {
AddressObjectDetails d = details;
AddressObject ao = entity;
ao.setActual(false);
ao.setIfnsCode(d.ifnsCode);
ao.setIfnsLocalCode(d.ifnsLocalCode);
ao.setOkato(d.okato);
ao.setParent(d.parentItem == 0 ? null : findAddressObject(d.parentItem));
if (d.parentItem != 0){//так обновляются дети
//Бубен, т.к. поле кода уникальное
Iterator it = findEntityWhat("a.kladrCode",
AddressObject.class,
new Field[]{new Field("actual", false),
new Field("parent", ao.getParent()),
new Field("kladrCode", "%.%", Field.OPERATOR_LIKE)},
"").iterator();
int c = 0;
boolean hasChildren = false;
while (it.hasNext()) {
hasChildren = true;
String s =(String)it.next();
int c1 = Integer.parseInt(s.substring(s.lastIndexOf(".")+1));
if (c1 > c){
c = c1;
}
}
if (hasChildren){
ao.setKladrCode(ao.getParent().getKladrCode() + "." + (c + 1));
}else{
ao.setKladrCode(ao.getParent().getKladrCode() + ".1");
}
}else{//так обновляются родители (страны)
String code = ao.getKladrCode();
ao.setKladrCode(d.kladrCode);
List<AddressObject> addressObjects
= findEntityList(AddressObject.class,
new Field[]{new Field("parent", null, Field.OPERATOR_NOT_NULL),
new Field("actual", false),
new Field("kladrCode", code + ".%", Field.OPERATOR_LIKE)});
if (ao.getId() != AddressObject.ADDRESS_OBJECT_RUSSIA){
for (AddressObject addressObject : addressObjects) {
String kladrCode = addressObject.getKladrCode();
int point = kladrCode.indexOf(".");
kladrCode = ao.getKladrCode() + kladrCode.substring(point);
addressObject.setKladrCode(kladrCode);
}
}
}
ao.setPostIndex(d.postIndex);
ao.setTitle(d.title);
//ao.setDirty(details.dirty);
ao.setType(findAddressObjectType(d.typeId));
}