public InstanceRef parseInstanceRefAndRegister(String fullId) throws InstantiationException, IllegalAccessException { EntityLoadInfo loadInfo; if (!fullId.startsWith("NEW-")) { loadInfo = EntityLoadInfo.parse(fullId); if (loadInfo == null) { throw new RuntimeException("Cannot parse id: " + fullId); } InstanceRef existingRef = instanceRefs.get(loadInfo.getMetaClass().getName() + "-" + loadInfo.getId().toString()); if (existingRef != null) { return existingRef; } } else { int idDashIndex = StringUtils.ordinalIndexOf(fullId, "-", 2); if (idDashIndex == -1) { String entityName = fullId.substring("NEW-".length()); String generatedId = generateId(entityName); fullId = fullId + "-" + generatedId; } loadInfo = EntityLoadInfo.parse(fullId); if (loadInfo == null) { throw new RuntimeException("Cannot parse id: " + fullId); } } if (loadInfo.isNewEntity()) newInstanceIds.add(loadInfo.toString()); InstanceRef result = new InstanceRef(loadInfo); instanceRefs.put(loadInfo.getMetaClass().getName() + "-" + loadInfo.getId().toString(), result); return result; }
public InstanceRef(EntityLoadInfo loadInfo) throws InstantiationException, IllegalAccessException { if (loadInfo == null) throw new NullPointerException("No load info passed"); this.loadInfo = loadInfo; MetaClass childMetaClass = loadInfo.getMetaClass(); if (!Strings.isNullOrEmpty(loadInfo.getViewName()) && !loadInfo.isNewEntity()) { DataService dataService = AppBeans.get(DataService.class); LoadContext<BaseGenericIdEntity<Object>> ctx = new LoadContext<>(loadInfo.getMetaClass()); ctx.setId(loadInfo.getId()).setView(loadInfo.getViewName()); instance = dataService.load(ctx); if (instance == null) { throw new RuntimeException("Entity with loadInfo " + loadInfo + " not found"); } } else { //noinspection unchecked instance = (BaseGenericIdEntity<Object>) AppBeans.get(Metadata.class).create(childMetaClass); if (!loadInfo.isNewEntity()) { for (MetaProperty metaProperty : childMetaClass.getProperties()) { if (!metaProperty.getRange().isClass()) { try { instance.setValue(metaProperty.getName(), null); } catch (Exception ignored) { } } } } } instance.setId(this.loadInfo.getId()); }
protected Entity loadEntityInstance(EntityLoadInfo info) { if (info.isNewEntity()) { return metadata.create(info.getMetaClass()); } //noinspection unchecked LoadContext<Entity> ctx = new LoadContext(info.getMetaClass()).setId(info.getId()); if (info.getViewName() != null) { View view = viewRepository.findView(info.getMetaClass(), info.getViewName()); if (view != null) { ctx.setView(view); } else { log.warn("Unable to find view \"{}\" for entity \"{}\"", info.getViewName(), info.getMetaClass()); } } Entity entity; try { entity = dataService.load(ctx); } catch (Exception e) { log.warn("Unable to load item: {}", info, e); return null; } return entity; }
protected Entity loadEntityInstance(EntityLoadInfo info) { Metadata metadata = AppBeans.get(Metadata.class); if (info.isNewEntity()) { return metadata.create(info.getMetaClass()); } LoadContext ctx = new LoadContext(info.getMetaClass()).setId(info.getId()); if (info.getViewName() != null) ctx.setView(info.getViewName()); Entity entity; try { DataManager dataManager = AppBeans.get(DataManager.class); entity = dataManager.load(ctx); } catch (Exception e) { throw new BpmException("Unable to load item: " + info, e); } return entity; } }