001package org.andromda.metafacades.uml14; 002 003import java.util.List; 004import org.andromda.core.translation.Expression; 005import org.andromda.core.translation.ExpressionTranslator; 006import org.andromda.metafacades.uml.UMLMetafacadeUtils; 007import org.andromda.translation.ocl.ExpressionKinds; 008import org.apache.commons.lang.StringEscapeUtils; 009import org.apache.commons.lang.StringUtils; 010import org.omg.uml.foundation.core.Constraint; 011 012/** 013 * Metafacade implementation for org.andromda.metafacades.uml.ConstraintFacade. 014 * 015 * @see org.andromda.metafacades.uml.ConstraintFacade 016 * @author Bob Fields 017 */ 018public class ConstraintFacadeLogicImpl 019 extends ConstraintFacadeLogic 020{ 021 private static final long serialVersionUID = 34L; 022 /** 023 * @param metaObject 024 * @param context 025 */ 026 public ConstraintFacadeLogicImpl( 027 Constraint metaObject, 028 String context) 029 { 030 super(metaObject, context); 031 } 032 033 /** 034 * @see org.andromda.metafacades.uml.ConstraintFacade#getBody() 035 */ 036 @Override 037 public String handleGetBody() 038 { 039 String body = null; 040 if (this.metaObject.getBody() != null) 041 { 042 body = this.metaObject.getBody().getBody(); 043 } 044 return body; 045 } 046 047 /** 048 * @see org.andromda.metafacades.uml.ConstraintFacade#getContextElement() 049 */ 050 @Override 051 public Object handleGetContextElement() 052 { 053 Object element = null; 054 final List elements = this.metaObject.getConstrainedElement(); 055 if (elements != null && !elements.isEmpty()) 056 { 057 element = elements.get(0); 058 } 059 return element; 060 } 061 062 /** 063 * @see org.andromda.metafacades.uml.ConstraintFacade#isInvariant() 064 */ 065 @Override 066 public boolean handleIsInvariant() 067 { 068 return UMLMetafacadeUtils.isConstraintKind( 069 this.getBody(), 070 ExpressionKinds.INV); 071 } 072 073 /** 074 * @see org.andromda.metafacades.uml.ConstraintFacade#isPreCondition() 075 */ 076 @Override 077 public boolean handleIsPreCondition() 078 { 079 return UMLMetafacadeUtils.isConstraintKind( 080 this.getBody(), 081 ExpressionKinds.PRE); 082 } 083 084 /** 085 * @see org.andromda.metafacades.uml.ConstraintFacade#isPostCondition() 086 */ 087 @Override 088 public boolean handleIsPostCondition() 089 { 090 return UMLMetafacadeUtils.isConstraintKind( 091 this.getBody(), 092 ExpressionKinds.POST); 093 } 094 095 /** 096 * @see org.andromda.metafacades.uml.ConstraintFacade#isDefinition() 097 */ 098 @Override 099 public boolean handleIsDefinition() 100 { 101 return UMLMetafacadeUtils.isConstraintKind( 102 this.getBody(), 103 ExpressionKinds.DEF); 104 } 105 106 /** 107 * @see org.andromda.metafacades.uml.ConstraintFacade#isBodyExpression() 108 */ 109 @Override 110 public boolean handleIsBodyExpression() 111 { 112 return UMLMetafacadeUtils.isConstraintKind( 113 this.getBody(), 114 ExpressionKinds.BODY); 115 } 116 117 /** 118 * @see org.andromda.metafacades.uml.ConstraintFacade#getTranslation(String) 119 */ 120 @Override 121 public String handleGetTranslation(String language) 122 { 123 final Expression expression = 124 ExpressionTranslator.instance().translate( 125 language, 126 this.getBody(), 127 this.getContextElement()); 128 return expression == null ? null : expression.getTranslatedExpression(); 129 } 130 131 /** 132 * @see org.andromda.metafacades.uml.ModelElementFacade#getDocumentation(String, int, boolean) 133 */ 134 public String getDocumentation( 135 String indent, 136 int lineLength, 137 boolean htmlStyle) 138 { 139 String documentation = super.getDocumentation(indent, lineLength, htmlStyle); 140 boolean isBlank; 141 142 if (htmlStyle) 143 { 144 final String plainDocumentation = super.getDocumentation(indent, lineLength, false); 145 isBlank = StringUtils.isBlank(plainDocumentation); 146 } 147 else 148 { 149 isBlank = StringUtils.isBlank(documentation); 150 } 151 152 if (isBlank) 153 { 154 documentation = "An undocumented constraint has been violated: " + StringEscapeUtils.escapeJava(getBody()); 155 } 156 return documentation; 157 } 158}