if (viewName == null || viewName.isEmpty()) {
throw new IllegalArgumentException("View name must be provided");
}
ViewRegistry viewRegistry = ViewRegistry.getInstance();
ViewInstanceEntity viewInstanceEntity = viewRegistry.getInstanceDefinition(viewName, version, name);
viewName = ViewEntity.getViewName(viewName, version);
if (viewInstanceEntity == null) {
viewInstanceEntity = new ViewInstanceEntity();
viewInstanceEntity.setName(name);
viewInstanceEntity.setViewName(viewName);
ViewEntity viewEntity = new ViewEntity();
viewEntity.setName(viewName);
viewEntity.setVersion(version);
viewInstanceEntity.setViewEntity(viewEntity);
}
if (properties.containsKey(LABEL_PROPERTY_ID)) {
viewInstanceEntity.setLabel((String) properties.get(LABEL_PROPERTY_ID));
}
if (properties.containsKey(DESCRIPTION_PROPERTY_ID)) {
viewInstanceEntity.setDescription((String) properties.get(DESCRIPTION_PROPERTY_ID));
}
String visible = (String) properties.get(VISIBLE_PROPERTY_ID);
viewInstanceEntity.setVisible(visible==null ? true : Boolean.valueOf(visible));
if (properties.containsKey(ICON_PATH_ID)) {
viewInstanceEntity.setIcon((String) properties.get(ICON_PATH_ID));
}
if (properties.containsKey(ICON64_PATH_ID)) {
viewInstanceEntity.setIcon64((String) properties.get(ICON64_PATH_ID));
}
Collection<ViewInstancePropertyEntity> instanceProperties = new HashSet<ViewInstancePropertyEntity>();
Collection<ViewInstanceDataEntity> instanceData = new HashSet<ViewInstanceDataEntity>();
for (Map.Entry<String, Object> entry : properties.entrySet()) {
String propertyName = entry.getKey();
if (propertyName.startsWith(PROPERTIES_PREFIX)) {
ViewInstancePropertyEntity viewInstancePropertyEntity = new ViewInstancePropertyEntity();
viewInstancePropertyEntity.setViewName(viewName);
viewInstancePropertyEntity.setViewInstanceName(name);
viewInstancePropertyEntity.setName(entry.getKey().substring(PROPERTIES_PREFIX.length()));
viewInstancePropertyEntity.setValue((String) entry.getValue());
viewInstancePropertyEntity.setViewInstanceEntity(viewInstanceEntity);
instanceProperties.add(viewInstancePropertyEntity);
} else if (propertyName.startsWith(DATA_PREFIX)) {
ViewInstanceDataEntity viewInstanceDataEntity = new ViewInstanceDataEntity();
viewInstanceDataEntity.setViewName(viewName);
viewInstanceDataEntity.setViewInstanceName(name);
viewInstanceDataEntity.setName(entry.getKey().substring(DATA_PREFIX.length()));
viewInstanceDataEntity.setValue((String) entry.getValue());
viewInstanceDataEntity.setViewInstanceEntity(viewInstanceEntity);
instanceData.add(viewInstanceDataEntity);
}
}
if (!instanceProperties.isEmpty()) {
viewInstanceEntity.setProperties(instanceProperties);
}
if (!instanceData.isEmpty()) {
viewInstanceEntity.setData(instanceData);
}
return viewInstanceEntity;
}