public class PACMultipleAnnotationDemo { @PageActivationContext(index=2) private Double two; @PageActivationContext(index=0) private String zero; @PageActivationContext(index=1) private Integer one;
private void validateSortedFields(List<PlasticField> sortedFields) { List<Integer> expectedIndexes = CollectionFactory.newList(); List<Integer> actualIndexes = CollectionFactory.newList(); Set<Boolean> activates = CollectionFactory.newSet(); Set<Boolean> passivates = CollectionFactory.newSet(); for (int i = 0; i < sortedFields.size(); ++i) { PlasticField field = sortedFields.get(i); PageActivationContext annotation = field.getAnnotation(PageActivationContext.class); expectedIndexes.add(i); actualIndexes.add(annotation.index()); activates.add(annotation.activate()); passivates.add(annotation.passivate()); } List<String> errors = CollectionFactory.newList(); if (!expectedIndexes.equals(actualIndexes)) { errors.add(String.format("Index values must start at 0 and increment by 1 (expected [%s], found [%s])", InternalUtils.join(expectedIndexes), InternalUtils.join(actualIndexes))); } if (activates.size() > 1) { errors.add("Illegal values for 'activate' (all fields must have the same value)"); } if (passivates.size() > 1) { errors.add("Illegal values for 'passivate' (all fields must have the same value)"); } if (!errors.isEmpty()) { throw new RuntimeException(String.format("Invalid values for @PageActivationContext: %s", InternalUtils.join(errors))); } }
private void transformFields(TransformationSupport support, List<PlasticField> fields) { List<PlasticField> sortedFields = CollectionFactory.newList(fields); Collections.sort(sortedFields, INDEX_COMPARATOR); validateSortedFields(sortedFields); PlasticField firstField = sortedFields.get(0); PageActivationContext firstAnnotation = firstField.getAnnotation(PageActivationContext.class); // these arrays reduce memory usage and allow the PlasticField instances to be garbage collected FieldHandle[] handles = new FieldHandle[sortedFields.size()]; String[] typeNames = new String[sortedFields.size()]; int i = 0; for (PlasticField field : sortedFields) { handles[i] = field.getHandle(); typeNames[i] = field.getTypeName(); ++i; } if (firstAnnotation.activate()) { support.addEventHandler(EventConstants.ACTIVATE, 1, "PageActivationContextWorker activate event handler", createActivationHandler(handles, typeNames)); } if (firstAnnotation.passivate()) { support.addEventHandler(EventConstants.PASSIVATE, 0, "PageActivationContextWorker passivate event handler", createPassivateHandler(handles)); } // We don't claim the field, and other workers may even replace it with a FieldConduit. }
public int compare(PlasticField field1, PlasticField field2) { int index1 = field1.getAnnotation(PageActivationContext.class).index(); int index2 = field2.getAnnotation(PageActivationContext.class).index(); int compare = index1 < index2 ? -1 : (index1 > index2 ? 1 : 0); if (compare == 0) { compare = field1.getName().compareTo(field2.getName()); } return compare; } };
@UnknownActivationContextCheck public class OneContext { @PageActivationContext private String activationContext; }
public class EncodeTransientEntities { @PageActivationContext @Property private User user; void onActivate() { if (user == null) { user = new User(); } } }
public class EncodeTransientEntities { @PageActivationContext @Property private User user; void onActivate() { if (user == null) { user = new User(); } } }
@UnknownActivationContextCheck public class KnownActivationContextDemo { @PageActivationContext @Property private String activationContext; }
public class PACAnnotationDemo { @Property @PageActivationContext private Integer count; @Property private boolean countSet; void onActivate() { countSet = count != null; } }
public class MusicDetails2 { @Property @PageActivationContext private Track track; @InjectPage private Index index; Object onActivate() { if (track == null) { index.setAlert("Track not specified in URL."); return index; } return null; } }
public class View { @PageActivationContext @Property private String content; @Persist @Property private int count; void pageReset() { count = 0; } void onActionFromIncrement() { count++; } }