/* * (c) Copyright 2010-2011 AgileBirds * * This file is part of OpenFlexo. * * OpenFlexo is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * OpenFlexo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>. * */ package org.openflexo.foundation.dm; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.logging.Logger; import org.openflexo.foundation.dm.DMProcessBusinessDataAccessingMethod.CodeType; import org.openflexo.foundation.wkf.FlexoProcess; import org.openflexo.foundation.xml.FlexoDMBuilder; import org.openflexo.toolbox.StringUtils; import org.openflexo.toolbox.ToolBox; /** * Represent an auto generated process business data entity * * @author ndaniels * */ public class AutoGeneratedProcessBusinessDataDMEntity extends DMEntity { private static final Logger logger = Logger.getLogger(AutoGeneratedProcessBusinessDataDMEntity.class.getPackage().getName()); public static String getProcessBusinessDataEntityNameForProcess(FlexoProcess process) { return ToolBox.getJavaClassName(process.getName() + "BusinessData"); } /** * Constructor used during deserialization */ public AutoGeneratedProcessBusinessDataDMEntity(FlexoDMBuilder builder) { super(builder.dmModel); initializeDeserialization(builder); } /** * Constructor used for dynamic creation */ public AutoGeneratedProcessBusinessDataDMEntity(ProcessBusinessDataRepository repository, FlexoProcess process) { super(repository.getDMModel(), getProcessBusinessDataEntityNameForProcess(process), repository.getBusinessDataPackage().getName(), getProcessBusinessDataEntityNameForProcess(process), DMType.makeResolvedDMType(repository.getProcessBusinessDataEntity())); repository.registerEntity(this); } /** * Generates all properties for the specified process. * * @param process * @return All DMProperty/DMMethods in this entity linked to this process (basically all properties for which this method retrieved or * created a property) */ public Set<DMObject> updateProcessBusinessDataProperties(FlexoProcess process) { Set<DMObject> propertiesToKept = new HashSet<DMObject>(); for (Map.Entry<String, Class<? extends Object>> keyEntry : process.getProcessInstanceDictionaryKeys().entrySet()) { propertiesToKept.add(createPropertyForProcessKey(keyEntry.getKey(), keyEntry.getValue())); } // Generate method to access to the parent process business data propertiesToKept.addAll(createPropertiesForParentProcessBusinessData(process)); return propertiesToKept; } /** * Create a property with the specified key and the specified type if it does not exist yet * * @param key * @param type * @return the created property or the existing one */ private DMProperty createPropertyForProcessKey(String key, Class<? extends Object> type) { DMProperty property = getDMProperty(key); if (property == null) { property = createDMProperty(key, DMType.makeResolvedDMType(type, getProject()), DMPropertyImplementationType.PUBLIC_ACCESSORS_ONLY); property.setIsReadOnly(true); } return property; } /** * Create a getter for the specified process parent business data and a property for the parent process id (if it has parent and if the * parent has business data) * * @param process * @param propertiesToKept * return a set of properties to kept */ private Set<DMObject> createPropertiesForParentProcessBusinessData(FlexoProcess process) { Set<DMObject> propertiesToKept = new HashSet<DMObject>(); if (process.getParentProcess() != null && process.getParentProcess().getBusinessDataType() != null) { propertiesToKept.add(createPropertyForProcessKey(getParentProcessBusinessDataIdKey(process.getParentProcess()), Integer.class)); DMProcessBusinessDataAccessingMethod foundMethod = DMProcessBusinessDataAccessingMethod.getProcessBusinessDataAccessingMethod( this, process.getParentProcess(), CodeType.AUTOGENERATEDBUSINESSDATA_PARENT); if (foundMethod == null) { foundMethod = new DMProcessBusinessDataAccessingMethod(this, process.getParentProcess(), CodeType.AUTOGENERATEDBUSINESSDATA_PARENT); } else { foundMethod.updateProcess(process.getParentProcess()); } propertiesToKept.add(foundMethod); } return propertiesToKept; } /** * Create a getter for the specified process single process children business data and a property for the children process id (for each * child process with a business data) <br/> * Need to be called when the children process business data are set ! * * @param process * @param propertiesToKept * return a set of properties to kept */ public Set<DMObject> createPropertiesForChildrenProcessBusinessData(FlexoProcess process) { Set<DMObject> propertiesToKept = new HashSet<DMObject>(); for (FlexoProcess childProcess : process.getSubProcesses()) { if (childProcess.getBusinessDataType() != null && childProcess.isSingleSubProcess()) { DMProcessBusinessDataAccessingMethod foundMethod = DMProcessBusinessDataAccessingMethod .getProcessBusinessDataAccessingMethod(this, childProcess, CodeType.AUTOGENERATEDBUSINESSDATA_CHILD); if (foundMethod == null) { foundMethod = new DMProcessBusinessDataAccessingMethod(this, childProcess, CodeType.AUTOGENERATEDBUSINESSDATA_CHILD); } else { foundMethod.updateProcess(childProcess); } propertiesToKept.add(foundMethod); } } return propertiesToKept; } /** * @see org.openflexo.foundation.dm.DMEntity#getPropertyDefaultImplementationType() */ @Override public DMPropertyImplementationType getPropertyDefaultImplementationType() { return DMPropertyImplementationType.PUBLIC_ACCESSORS_ONLY; } public static String getGetterDefaultCoreCodeForProperty(DMProperty property) { StringBuilder sb = new StringBuilder(); sb.append(" {" + StringUtils.LINE_SEPARATOR); sb.append("\treturn (" + property.getAccessorTypeAsString() + ")objectForKey(\"" + property.getName() + "\");"); sb.append(StringUtils.LINE_SEPARATOR + "}"); return sb.toString(); } public static String getSetterDefaultCoreCodeForProperty(DMProperty property) { StringBuilder sb = new StringBuilder(); sb.append(" {" + StringUtils.LINE_SEPARATOR); sb.append("\tsetObjectForKey(" + property.getNameAsMethodArgument() + ", \"" + property.getName() + "\");"); sb.append(StringUtils.LINE_SEPARATOR + "}"); return sb.toString(); } @Override public boolean isDeletable() { return true; } /** * Tells if code generation is applicable for related DMEntity * * @return */ @Override public boolean isCodeGenerationApplicable() { return true; } public static String getParentProcessBusinessDataIdKey(FlexoProcess process) { return getParentProcessBusinessDataIdKey(process.getBusinessDataDictionaryKey()); } public static String getParentProcessBusinessDataIdKey(String processKey) { return processKey + "Id"; } }