private int move(DctmElement target, DctmElement element, int cnt) throws CaoException {
if (monitor.isCanceled()) return cnt;
try {
IDfSysObject src = (IDfSysObject) session.getObject(new DfId(element.getId()));
IDfFolder trg = (IDfFolder) session.getObject(new DfId(target.getId()));
monitor.log().debug("Move " + src.getObjectId() + " to " + trg.getObjectId());
if (src instanceof IDfFolder) {
do {
String dql = "dm_folder WHERE FOLDER(ID('"+trg.getObjectId()+"')) and object_name='"+MSql.escape(src.getObjectName())+"'";
IDfPersistentObject existing = session.getObjectByQualification(dql);
if ( existing != null ) {
// Target folder already exists - this is the short way, use the existing folder to continue
monitor.log().debug(" Target folder already exists " + existing.getObjectId());
int nextIndex = 1;
String name = src.getObjectName();
int pos = name.lastIndexOf('_');
if (pos > 0 && MCast.toint(name.substring(pos+1),0) > 0) {
nextIndex = MCast.toint(name.substring(pos+1),0)+1;
name = name.substring(0,pos);
}
name = name + "_" + nextIndex;
src.setObjectName(name);
src.save();
events.add(new String[] { "c", src.getObjectId().getId(), "" } );
} else
break;
} while (true);
}
LinkedList<String> tmp = new LinkedList<String>();
for ( int i = 0; i < src.getFolderIdCount(); i++) {
String folderId = src.getFolderId(i).getId();
tmp.add(folderId);
events.add(new String[] { "u", src.getObjectId().getId(), folderId } );
}
src.link(trg.getObjectId().getId());
events.add(new String[] { "l", src.getObjectId().getId(), trg.getObjectId().getId() } );
for (String id : tmp )
src.unlink(id);
src.save();