/* * (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.logging.Level; import org.openflexo.foundation.dm.javaparser.MethodSourceCode; import org.openflexo.foundation.wkf.FlexoProcess; import org.openflexo.foundation.xml.FlexoDMBuilder; import org.openflexo.toolbox.StringUtils; import org.openflexo.toolbox.ToolBox; /** * @author nid * */ public class DMProcessBusinessDataAccessingMethod extends DMMethod { public static enum CodeType { CUSTOMCOMPONENT_CURRENTBUSINESSDATA, AUTOGENERATEDBUSINESSDATA_PARENT, AUTOGENERATEDBUSINESSDATA_CHILD } private long processId; private String processKey; private CodeType codeType; private boolean isSourceCodeCorrectlyInitialized = false; /** * Constructor used during deserialization */ public DMProcessBusinessDataAccessingMethod(FlexoDMBuilder builder) { super(builder); } /** * Constructor used for dynamic creation */ public DMProcessBusinessDataAccessingMethod(DMEntity entity, FlexoProcess process) { this(entity, process, null); } /** * Constructor used for dynamic creation */ public DMProcessBusinessDataAccessingMethod(DMEntity entity, FlexoProcess process, CodeType codeType) { super(entity.getDMModel()); this.codeType = codeType; entity.registerMethod(this); updateProcess(process); } /** * @see org.openflexo.foundation.dm.DMMethod#getIsReadOnly() */ @Override public boolean getIsReadOnly() { return true; } /** * @see org.openflexo.foundation.dm.DMMethod#isDeletable() */ @Override public boolean isDeletable() { return false; } /** * @see org.openflexo.foundation.dm.DMMethod#getIsStaticallyDefinedInTemplate() */ @Override public boolean getIsStaticallyDefinedInTemplate() { return false; } /** * @see org.openflexo.foundation.dm.DMMethod#getDefaultCoreCode() */ @Override String getDefaultCoreCode() { if (getReturnType() != null && _isRegistered) { isSourceCodeCorrectlyInitialized = true; if (codeType != null) { switch (codeType) { case CUSTOMCOMPONENT_CURRENTBUSINESSDATA: return "return (" + getReturnType().getSimplifiedStringRepresentation() + ")" + ProcessBusinessDataRepository.getCustomComponentCurrentProcessBusinessDataGetterName() + "(\"" + processKey + "\");"; case AUTOGENERATEDBUSINESSDATA_CHILD: StringBuilder sb = new StringBuilder(); sb.append("Vector<?> list = " + ProcessBusinessDataRepository.getChildrenProcessBusinessDataGetterName() + "(\"" + processKey + "\");"); sb.append(StringUtils.LINE_SEPARATOR + "if(list.size() > 0) return (" + getReturnType().getSimplifiedStringRepresentation() + ") list.get(0);"); sb.append(StringUtils.LINE_SEPARATOR + "return null;"); return sb.toString(); case AUTOGENERATEDBUSINESSDATA_PARENT: return "return (" + getReturnType().getSimplifiedStringRepresentation() + ")" + ProcessBusinessDataRepository.getParentProcessBusinessDataGetterName() + "(\"" + processKey + "\");"; } } } return super.getDefaultCoreCode(); } /** * @see org.openflexo.foundation.dm.DMMethod#getSourceCode() */ @Override public MethodSourceCode getSourceCode() { try { if (!isSourceCodeCorrectlyInitialized) { resetSourceCode(); } } catch (Exception e) { // Should never happen logger.log(Level.SEVERE, "Cannot reste code for DMProcessBusinessDataAccessingMethod", e); } return super.getSourceCode(); } /** * @see org.openflexo.foundation.dm.DMMethod#setEntity(org.openflexo.foundation.dm.DMEntity) */ @Override public void setEntity(DMEntity entity) { boolean needUpdateCode = this.entity != entity; super.setEntity(entity); if (needUpdateCode) { getSourceCode().updateComputedCode(); } } public void updateProcess(FlexoProcess process) { if (process.getBusinessDataType() == null) { delete(); } else { try { this.processKey = process.getProcessDictionaryKey(); this.processId = process.getFlexoID(); setName(getAccessingBusinessDataMethodName(process)); setReturnType(DMType.makeResolvedDMType(process.getBusinessDataType())); } catch (DuplicateMethodSignatureException e) { logger.log(Level.WARNING, "Cannot change process business data accessing method name to " + getAccessingBusinessDataMethodName(process), e); } } } public long getProcessId() { return processId; } /** * Should not be used directly, use updateProcess instead ! * * @param processId */ public void setProcessId(long processId) { this.processId = processId; } public String getProcessKey() { return processKey; } /** * Should not be used directly, use updateProcess instead ! * * @param processId */ public void setProcessKey(String processKey) { this.processKey = processKey; } public CodeType getCodeType() { return codeType; } /** * Should not be used directly, use the appropriate constructor instead ! * * @param codeType */ public void setCodeType(CodeType codeType) { this.codeType = codeType; } public static DMProcessBusinessDataAccessingMethod getProcessBusinessDataAccessingMethod(DMEntity entity, FlexoProcess process, CodeType codeType) { for (DMMethod method : entity.getDeclaredMethods()) { if (method instanceof DMProcessBusinessDataAccessingMethod && ((DMProcessBusinessDataAccessingMethod) method).getProcessId() == process.getFlexoID() && ((DMProcessBusinessDataAccessingMethod) method).getCodeType() == codeType) { return (DMProcessBusinessDataAccessingMethod) method; } } return null; } public static String getAccessingBusinessDataMethodName(FlexoProcess process) { return getAccessingBusinessDataMethodName(process.getProcessDictionaryKey()); } public static String getAccessingBusinessDataMethodName(String processKey) { return "get" + ToolBox.getJavaClassName(processKey) + "BusinessData"; } }