cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate attribute for every model element

Former Member
0 Kudos

I have a context with a model node Control_List. I want to use a calculated attribute MonthCalc that returns a string representing the name of the month stored in a Date model element. I added a child value node with cardinality 1...n and singleton=false to the model node, and defined a calculated value element, but the method is only called once.

What have I done wrong?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Why cardinality 1:N and not 1:1? How does the code inside the getter looks like?

Armin

Former Member
0 Kudos

Quickly tried it with cardinality 1:1, that does not make a difference.


public java.lang.String getCalculatedMonthCalc(IPrivateRacagnacs.ICalculatedElement element)

{

//@@begin getCalculatedMonthCalc(IPrivateRacagnacs.ICalculatedElement)
String[] months = { "January","February","March","April","May","June","July","August","September","October","November","December" };

Calendar cal = new GregorianCalendar();
cal.setTime(wdContext.currentControl_ListElement().getDatum());
return months[cal.get(Calendar.MONTH)];
//@@end

}

Former Member
0 Kudos

Several remarks.

First, you should calculate the month name from the date value stored in the parent element. Thus, change the code to


IControl_ListElement parentElement = (IControl_ListElement) element.node().getParentElement();
cal.setTime( parentElement.getDatum() );

Second, don't hardcode the month names in this method, use the date formatting from JDK instead.

Armin

Former Member
0 Kudos

Hi

Use the following code to format the date

static public String getFormattedByMonth(Date date , Locale locale)

{

String result;

SimpleDateFormat format = new SimpleDateFormat("MMMMM", locale);

result = format.format(date);

return result;

}

Regards

Ayyapparaj

Former Member
0 Kudos

Well, then we should give all the missing details.


//@@begin others
private DateFormat monthFmt;
//@@end

wdDoInit()
{
  this.monthFmt = new SimpleDateFormat("MMMM", WDClientUser.getCurrentUser().getLocale());
}

String get<CalculatedAttributeName>(...)
{
  IControl_ListElement parentElement = (IControl_ListElement) element.node().getParentElement();
  return monthFmt.format( parentElement.getDatum() );
}

Armin

Answers (0)