2013-02-13 18:26:22 +01:00
|
|
|
package org.gcube.portlets.user.reportgenerator.client.targets;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2014-01-22 18:31:02 +01:00
|
|
|
import java.util.List;
|
2013-02-13 18:26:22 +01:00
|
|
|
|
2013-03-01 19:42:50 +01:00
|
|
|
import org.gcube.portlets.d4sreporting.common.shared.BasicComponent;
|
2013-03-05 19:47:10 +01:00
|
|
|
import org.gcube.portlets.d4sreporting.common.shared.ComponentType;
|
2014-01-22 18:31:02 +01:00
|
|
|
import org.gcube.portlets.d4sreporting.common.shared.Metadata;
|
2013-03-01 19:42:50 +01:00
|
|
|
import org.gcube.portlets.d4sreporting.common.shared.RepeatableSequence;
|
2013-02-13 18:26:22 +01:00
|
|
|
import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter;
|
|
|
|
import org.gcube.portlets.user.reportgenerator.client.model.TemplateComponent;
|
|
|
|
|
2014-01-23 19:11:55 +01:00
|
|
|
import com.google.gwt.core.shared.GWT;
|
2013-02-26 19:44:48 +01:00
|
|
|
import com.google.gwt.user.client.ui.Composite;
|
2013-02-13 18:26:22 +01:00
|
|
|
import com.google.gwt.user.client.ui.VerticalPanel;
|
|
|
|
|
2013-10-30 18:50:22 +01:00
|
|
|
public class ClientRepeatableSequence extends Composite implements ClientSequence {
|
2013-03-05 19:47:10 +01:00
|
|
|
protected ArrayList<TemplateComponent> groupedComponents = new ArrayList<TemplateComponent>();
|
2013-02-13 18:26:22 +01:00
|
|
|
|
2014-01-28 17:31:13 +01:00
|
|
|
private String id;
|
2014-01-22 18:31:02 +01:00
|
|
|
private List<Metadata> metas;
|
2013-03-05 19:47:10 +01:00
|
|
|
private VerticalPanel myPanel = new VerticalPanel();
|
2013-02-26 19:44:48 +01:00
|
|
|
private Presenter p;
|
2014-01-28 17:31:13 +01:00
|
|
|
private String key;
|
2013-03-05 19:47:10 +01:00
|
|
|
protected RepeatableSequence repSequence;
|
|
|
|
|
|
|
|
protected RepeatableSequence originalSequence = new RepeatableSequence();
|
2013-02-13 18:26:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor used by the system when reading the model
|
2013-03-05 19:47:10 +01:00
|
|
|
* @param sRS the sequence to repeat
|
2013-02-13 18:26:22 +01:00
|
|
|
*/
|
2013-03-01 19:42:50 +01:00
|
|
|
public ClientRepeatableSequence(Presenter p, RepeatableSequence sRS) {
|
2013-02-13 18:26:22 +01:00
|
|
|
this.p = p;
|
2013-03-05 19:47:10 +01:00
|
|
|
this.repSequence = sRS;
|
2014-01-28 17:31:13 +01:00
|
|
|
this.key = sRS.getKey();
|
2013-03-05 19:47:10 +01:00
|
|
|
this.originalSequence = extractOriginalSequence(sRS);
|
2013-12-06 12:35:06 +01:00
|
|
|
SequenceWidget seqW = new SequenceWidget(p, this, originalSequence, true, false);
|
2013-03-05 19:47:10 +01:00
|
|
|
myPanel.add(seqW);
|
2014-01-23 19:11:55 +01:00
|
|
|
|
2014-04-22 11:38:46 +02:00
|
|
|
ArrayList<RepeatableSequence> repeats = getRepeats();
|
|
|
|
if (repeats == null || repeats.size() == 0) {
|
|
|
|
seqW.showResetFields(true);
|
|
|
|
}
|
|
|
|
for (RepeatableSequence repeatSeq : repeats) {
|
2013-03-05 19:47:10 +01:00
|
|
|
addAnother(repeatSeq);
|
2013-02-13 18:26:22 +01:00
|
|
|
}
|
2014-04-22 11:38:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-26 19:44:48 +01:00
|
|
|
initWidget(myPanel);
|
2013-03-05 19:47:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-06 11:35:34 +01:00
|
|
|
* when you repeat a sequence the model is not able to recognize if this was a sequence or not
|
|
|
|
* to overcome this limitation you have to identify the original sequence
|
2013-03-05 19:47:10 +01:00
|
|
|
* to identify it you can use the REPEAT_SEQUENCE_DELIMITER ELEMENT
|
|
|
|
* @param sRS
|
|
|
|
*/
|
|
|
|
private RepeatableSequence extractOriginalSequence(RepeatableSequence sRS) {
|
|
|
|
RepeatableSequence toReturn = null;
|
|
|
|
int repeatDelimiterCounter = 2;
|
|
|
|
ArrayList<BasicComponent> groupedComponents = new ArrayList<BasicComponent>();
|
|
|
|
|
|
|
|
for (BasicComponent comp : sRS.getGroupedComponents()) {
|
|
|
|
if (comp.getType() == ComponentType.REPEAT_SEQUENCE_DELIMITER) {
|
|
|
|
repeatDelimiterCounter--;
|
2013-02-26 19:44:48 +01:00
|
|
|
}
|
2013-03-05 19:47:10 +01:00
|
|
|
if (comp.getType() != ComponentType.REPEAT_SEQUENCE_INNER)
|
|
|
|
groupedComponents.add(comp);
|
|
|
|
|
|
|
|
if (repeatDelimiterCounter == 0) {
|
|
|
|
toReturn = new RepeatableSequence(groupedComponents, sRS.getHeight());
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
2013-10-30 18:50:22 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2013-03-05 19:47:10 +01:00
|
|
|
private ArrayList<RepeatableSequence> getRepeats() {
|
|
|
|
ArrayList<RepeatableSequence> toReturn = new ArrayList<RepeatableSequence>();
|
|
|
|
int repeatDelimiterCounter = 2;
|
|
|
|
|
|
|
|
ArrayList<BasicComponent> groupedComponents = new ArrayList<BasicComponent>();
|
|
|
|
for (BasicComponent comp : repSequence.getGroupedComponents()) {
|
|
|
|
if (comp.getType() == ComponentType.REPEAT_SEQUENCE_DELIMITER) {
|
|
|
|
repeatDelimiterCounter--;
|
|
|
|
}
|
|
|
|
if (comp.getType() != ComponentType.REPEAT_SEQUENCE_INNER)
|
|
|
|
groupedComponents.add(comp);
|
|
|
|
|
|
|
|
if (repeatDelimiterCounter == 0) {
|
|
|
|
toReturn.add(new RepeatableSequence(groupedComponents, repSequence.getHeight()));
|
|
|
|
repeatDelimiterCounter = 2;
|
2014-01-23 19:11:55 +01:00
|
|
|
groupedComponents = new ArrayList<BasicComponent>();
|
2013-03-05 19:47:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//if there is only the first sequence return nothing
|
|
|
|
if (toReturn.size() <= 1) {
|
|
|
|
toReturn = new ArrayList<RepeatableSequence>();
|
|
|
|
}
|
|
|
|
else //return all of them without the first one
|
|
|
|
toReturn.remove(0);
|
|
|
|
return toReturn;
|
2013-02-13 18:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void add(TemplateComponent toAdd) {
|
2013-02-26 19:44:48 +01:00
|
|
|
if (toAdd.getType() != ComponentType.REPEAT_SEQUENCE_INNER) {
|
|
|
|
groupedComponents.add(toAdd);
|
|
|
|
myPanel.add(toAdd.getContent());
|
|
|
|
}
|
2013-02-13 18:26:22 +01:00
|
|
|
}
|
|
|
|
|
2013-03-05 19:47:10 +01:00
|
|
|
protected void addAnother(RepeatableSequence sRS) {
|
2013-03-06 19:06:42 +01:00
|
|
|
SequenceWidget seqW = new SequenceWidget(p, this, sRS, false, false);
|
|
|
|
myPanel.add(seqW);
|
|
|
|
}
|
2014-01-23 19:11:55 +01:00
|
|
|
|
2013-10-30 18:50:22 +01:00
|
|
|
|
2013-03-06 19:06:42 +01:00
|
|
|
protected void addNew(RepeatableSequence sRS) {
|
|
|
|
SequenceWidget seqW = new SequenceWidget(p, this, sRS, false, true);
|
2013-03-05 19:47:10 +01:00
|
|
|
myPanel.add(seqW);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* remove the widget and its components
|
|
|
|
* @param toRemove
|
|
|
|
*/
|
|
|
|
protected void removeSeqWidget(SequenceWidget toRemove) {
|
|
|
|
myPanel.remove(toRemove);
|
|
|
|
for (TemplateComponent tc : toRemove.getSeqGroupedComponents()) {
|
|
|
|
groupedComponents.remove(tc);
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 19:11:55 +01:00
|
|
|
/**
|
|
|
|
* add the template component in the model
|
|
|
|
*/
|
2013-10-30 18:50:22 +01:00
|
|
|
@Override
|
2014-01-23 19:11:55 +01:00
|
|
|
public void addTemplateComponent(TemplateComponent toAdd) {
|
|
|
|
groupedComponents.add(toAdd);
|
|
|
|
}
|
2014-01-22 18:31:02 +01:00
|
|
|
/**
|
2014-01-23 19:11:55 +01:00
|
|
|
* return the grouped components of a sequence. For the specific RSG case if it finds a @see {@link SequenceWidget} RSG_BINDING_PROPERTY
|
|
|
|
* with value -1 it update it to the max value so far (requirement for RSG needed to wrote back the report in the DB)
|
|
|
|
* Note that a Sequence can contain multiple sequences and they are separeted by componentType.REPEAT_SEQUENCE_DELIMITER
|
2014-01-22 18:31:02 +01:00
|
|
|
*/
|
2014-01-23 19:11:55 +01:00
|
|
|
@Override
|
|
|
|
public ArrayList<TemplateComponent> getGroupedComponents() {
|
|
|
|
int maxBindingContext = getMaxIndex();
|
|
|
|
for (int i = 0; i < groupedComponents.size(); i++) {
|
|
|
|
TemplateComponent co = groupedComponents.get(i);
|
|
|
|
|
|
|
|
if (co.getType() == ComponentType.REPEAT_SEQUENCE_DELIMITER) {
|
|
|
|
maxBindingContext++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//specific for report References in Sequences
|
|
|
|
if (co.getType() == ComponentType.REPORT_REFERENCE) {
|
|
|
|
GWT.log("Reference found");
|
|
|
|
ClientReportReference refGroup = (ClientReportReference) co.getContent();
|
|
|
|
if (refGroup != null) {
|
2014-01-29 17:34:57 +01:00
|
|
|
boolean keepLooking = true;
|
2014-01-23 19:11:55 +01:00
|
|
|
for (Metadata prop : refGroup.getMetadata()) {
|
|
|
|
if ( prop.getAttribute().equals(SequenceWidget.RSG_BINDING_PROPERTY) && (extractIndexFromBindingContext(prop.getValue()) == -1) ) {
|
|
|
|
prop.setValue(replaceIndexFromBindingContext(prop.getValue(), maxBindingContext));
|
2014-01-29 17:34:57 +01:00
|
|
|
keepLooking = false;
|
2014-01-23 19:11:55 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-01-29 17:34:57 +01:00
|
|
|
}
|
|
|
|
if (keepLooking) {
|
|
|
|
for (Metadata prop : refGroup.getMetadata()) {
|
|
|
|
GWT.log("-" + prop.getAttribute()+":"+prop.getValue());
|
|
|
|
//if there's a new component
|
|
|
|
if ( prop.getAttribute().equals(SequenceWidget.RSG_BINDING_CONTEXT_PROPERTY) && (extractIndexFromBindingContext(prop.getValue()) == -1) ) {
|
|
|
|
prop.setValue(replaceIndexFromBindingContext(prop.getValue(), maxBindingContext));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 18:31:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 19:27:23 +01:00
|
|
|
if (co.getAllMetadata() != null) {
|
|
|
|
//for any other component
|
|
|
|
for (Metadata prop : co.getAllMetadata()) {
|
|
|
|
//if there's a new component
|
|
|
|
if ( prop.getAttribute().equals(SequenceWidget.RSG_BINDING_CONTEXT_PROPERTY) && (extractIndexFromBindingContext(prop.getValue()) == -1) ) {
|
|
|
|
prop.setValue(replaceIndexFromBindingContext(prop.getValue(), maxBindingContext));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 18:31:02 +01:00
|
|
|
}
|
2014-01-23 19:11:55 +01:00
|
|
|
return groupedComponents;
|
2014-01-22 18:31:02 +01:00
|
|
|
}
|
2014-01-23 19:11:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param binding it expect sth like #.profileList[0] or #.specificMeasureList[0] etc
|
|
|
|
* and return the integer into square brackets
|
|
|
|
* @return the integer into square brackets
|
|
|
|
*/
|
2014-01-22 18:31:02 +01:00
|
|
|
private int extractIndexFromBindingContext(String binding) {
|
2014-01-29 17:34:57 +01:00
|
|
|
if (binding.contains("[") && binding.contains("]")) {
|
|
|
|
int openSquareBracket = binding.lastIndexOf("[")+1;
|
|
|
|
int closeSquareBracket = binding.lastIndexOf("]");
|
|
|
|
return Integer.parseInt(binding.substring(openSquareBracket, closeSquareBracket));
|
|
|
|
}
|
|
|
|
return 0;
|
2014-01-22 18:31:02 +01:00
|
|
|
}
|
2014-01-23 19:11:55 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param binding it expect sth like #.profileList[0] or #.specificMeasureList[0] etc
|
|
|
|
* @param newIndex
|
|
|
|
* @return return the newIndex into square brackets
|
|
|
|
*/
|
2014-01-22 18:31:02 +01:00
|
|
|
private String replaceIndexFromBindingContext(String binding, int newIndex) {
|
2014-01-29 17:34:57 +01:00
|
|
|
if (binding.contains("[") && binding.contains("]")) {
|
|
|
|
int openSquareBracket = binding.lastIndexOf("[")+1;
|
|
|
|
int closeSquareBracket = binding.lastIndexOf("]");
|
|
|
|
String toReturn = binding.substring(0, openSquareBracket) + newIndex + binding.substring(closeSquareBracket);
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
return binding;
|
2014-01-22 18:31:02 +01:00
|
|
|
}
|
2014-01-23 19:11:55 +01:00
|
|
|
/**
|
|
|
|
* @return specific cor RSG Case search for binding="geoRefList[3]" and return the max number into the square brackets
|
|
|
|
*/
|
|
|
|
private int getMaxIndex() {
|
|
|
|
int max = 0;
|
2014-01-30 19:27:23 +01:00
|
|
|
for (TemplateComponent co : groupedComponents) {
|
|
|
|
if (co.getAllMetadata() != null) {
|
|
|
|
for (Metadata prop : co.getAllMetadata()) {
|
|
|
|
if (prop.getAttribute().equals(SequenceWidget.RSG_BINDING_CONTEXT_PROPERTY) || prop.getAttribute().equals(SequenceWidget.RSG_BINDING_PROPERTY)) {
|
|
|
|
int currValue = extractIndexFromBindingContext(prop.getValue());
|
|
|
|
if (currValue > max)
|
|
|
|
max = currValue;
|
|
|
|
}
|
2014-01-23 19:11:55 +01:00
|
|
|
}
|
2014-01-30 19:27:23 +01:00
|
|
|
} else {
|
|
|
|
GWT.log("getAllMetadata() null for co=" +co.getType());
|
2014-01-23 19:11:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
GWT.log("return max=" +max);
|
|
|
|
return max;
|
|
|
|
}
|
2013-02-13 18:26:22 +01:00
|
|
|
public void setGroupedComponents(ArrayList<TemplateComponent> groupedComponents) {
|
|
|
|
this.groupedComponents = groupedComponents;
|
2013-10-30 18:50:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-12-17 19:19:51 +01:00
|
|
|
public boolean add(String id, RepeatableSequence sequence, boolean isSingleRelation) {
|
2013-10-30 18:50:22 +01:00
|
|
|
SequenceWidget seqW = new SequenceWidget(p, this, sequence, false, true);
|
|
|
|
myPanel.add(seqW);
|
2014-04-22 11:38:46 +02:00
|
|
|
//disable clear content
|
|
|
|
if (myPanel.getWidgetCount() > 1) {
|
|
|
|
SequenceWidget first = (SequenceWidget) myPanel.getWidget(0);
|
|
|
|
first.showResetFields(false);
|
|
|
|
}
|
2013-10-30 18:50:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SequenceWidget remove(SequenceWidget toRemove) {
|
|
|
|
myPanel.remove(toRemove);
|
|
|
|
for (TemplateComponent tc : toRemove.getSeqGroupedComponents()) {
|
|
|
|
groupedComponents.remove(tc);
|
|
|
|
}
|
2014-04-22 11:38:46 +02:00
|
|
|
//enable clear content
|
|
|
|
if (myPanel.getWidgetCount() == 1) {
|
|
|
|
SequenceWidget first = (SequenceWidget) myPanel.getWidget(0);
|
|
|
|
first.showResetFields(true);
|
|
|
|
}
|
2013-10-30 18:50:22 +01:00
|
|
|
return toRemove;
|
2013-11-14 16:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void AddButtonClicked(RepeatableSequence sequence) {
|
2013-12-17 19:19:51 +01:00
|
|
|
add("not needed", sequence, false);
|
2013-11-18 18:25:49 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:31:13 +01:00
|
|
|
public String getKey() {
|
|
|
|
return key;
|
2014-01-22 18:31:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Metadata> getMetadata() {
|
|
|
|
return metas;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMetadata(List<Metadata> metas) {
|
|
|
|
this.metas = metas;
|
2014-01-23 19:11:55 +01:00
|
|
|
}
|
2014-01-29 17:34:57 +01:00
|
|
|
|
2014-01-28 17:31:13 +01:00
|
|
|
public String getId() {
|
|
|
|
if (id == null)
|
|
|
|
return "-1";
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setId(String id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
2014-01-23 19:11:55 +01:00
|
|
|
|
2014-01-29 17:34:57 +01:00
|
|
|
@Override
|
|
|
|
public void cleanInModel() {
|
|
|
|
//not needed
|
|
|
|
}
|
2013-02-13 18:26:22 +01:00
|
|
|
}
|