use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.
the class RuntimeWebComponent method get.
@Override
public Object get(final String name, final Scriptable start) {
if (specProperties != null && specProperties.contains(name)) {
PropertyDescription pd = webComponentSpec.getProperties().get(name);
if (WebFormComponent.isDesignOnlyProperty(pd))
return Scriptable.NOT_FOUND;
return NGConversions.INSTANCE.convertSabloComponentToRhinoValue(component.getProperty(name), pd, component, start);
}
if ("getFormName".equals(name)) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
IWebFormUI parent = component.findParent(IWebFormUI.class);
if (parent != null) {
return parent.getController().getName();
}
return null;
}
};
}
if ("getDesignTimeProperty".equals(name) && component.getFormElement().getPersistIfAvailable() instanceof AbstractBase) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
return Utils.parseJSExpression(((AbstractBase) component.getFormElement().getPersistIfAvailable()).getCustomDesignTimeProperty((String) args[0]));
}
};
}
if ("getDesignProperties".equals(name) && component.getFormElement().getPersistIfAvailable() instanceof AbstractBase) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Map<String, Object> designProperties = ((AbstractBase) component.getFormElement().getPersistIfAvailable()).getMergedCustomDesignTimeProperties();
Map<String, Object> parsedMap = new HashMap<String, Object>();
designProperties.entrySet().forEach(entry -> {
parsedMap.put(entry.getKey(), Utils.parseJSExpression(entry.getValue()));
});
return parsedMap;
}
};
}
final Function func = apiFunctions.get(name);
if (func != null && isApiFunctionEnabled(name)) {
final List<Pair<String, String>> oldVisibleForms = getVisibleForms();
return new BaseFunction() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object retValue;
cx.putThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE, Boolean.TRUE);
try {
retValue = func.call(cx, scope, thisObj, args);
} finally {
cx.removeThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE);
}
if (!(func instanceof WebComponentFunction)) {
WebObjectFunctionDefinition def = webComponentSpec.getApiFunctions().get(name);
retValue = NGConversions.INSTANCE.convertServerSideRhinoToRhinoValue(retValue, def.getReturnType(), component, null);
}
updateVisibleContainers(oldVisibleForms);
return retValue;
}
};
}
// check if we have a setter/getter for this property
if (name != null && name.length() > 0) {
String uName = new StringBuffer(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString();
if (apiFunctions.containsKey("set" + uName) && apiFunctions.containsKey("get" + uName)) {
// call getter
Function propertyGetter = apiFunctions.get("get" + uName);
return propertyGetter.call(Context.getCurrentContext(), start, start, new Object[] {});
}
}
if ("svyMarkupId".equals(name)) {
String formName = null;
IWebFormUI parent = component.findParent(IWebFormUI.class);
if (parent != null) {
formName = parent.getController().getName();
} else {
formName = component.getFormElement().getForm().getName();
}
return ComponentFactory.getMarkupId(formName, component.getName());
}
// is this really needed? will not prototype be looked at automatically by Rhino code?
if (prototypeScope != null) {
return prototypeScope.get(name, start);
}
return Scriptable.NOT_FOUND;
}
use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.
the class WebFormComponent method findCorrectFormEvenForServoyFormComponentChildren.
private Form findCorrectFormEvenForServoyFormComponentChildren() {
Form formElementForm = null;
IPersist persist = formElement.getPersistIfAvailable();
if (persist instanceof AbstractBase) {
String formName = ((AbstractBase) persist).getRuntimeProperty(FormElementHelper.FORM_COMPONENT_FORM_NAME);
if (formName != null) {
formElementForm = dataAdapterList.getApplication().getFormManager().getForm(formName).getForm();
}
}
if (formElementForm == null) {
formElementForm = formElement.getForm();
}
return formElementForm;
}
use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.
the class FormElementHelper method compareTabSeq.
// TODO: try to make this method recursive, for the cases when there are more than 2 nested form components
public static int compareTabSeq(int seq1, Object o1, int seq2, Object o2, FlattenedSolution flattenedSolution) {
int yxCompare = 0;
if (seq1 == ISupportTabSeq.DEFAULT && seq2 == ISupportTabSeq.DEFAULT && o1 instanceof IPersist && o2 instanceof IPersist) {
IPersist form = ((IPersist) o1).getAncestor(IRepository.FORMS);
if (form instanceof Form && ((Form) form).isResponsiveLayout()) {
if (((IPersist) o1).getParent().equals(((IPersist) o2).getParent())) {
// delegate to Yx
yxCompare = PositionComparator.YX_PERSIST_COMPARATOR.compare((IPersist) o1, (IPersist) o2);
} else {
/*
* We must search all the parents of the objects o1 and o2.If the objects have a same parent by searching all the ancestors we must compare
* the objects before encountering the same parent.
*/
List<IPersist> parentsOfo1 = new ArrayList<IPersist>();
IPersist parent = ((IPersist) o1).getParent();
while (!(parent instanceof Form)) {
parentsOfo1.add(parent);
parent = parent.getParent();
}
// also add the form to the list of parents
parentsOfo1.add(parent);
// the last parent of o1 or o2 is a formComponent, not the main form
if (parent instanceof Form) {
if (((Form) parent).isFormComponent() && o1 instanceof AbstractBase) {
String uuid = ((AbstractBase) o1).getRuntimeProperty(FORM_COMPONENT_UUID);
if (uuid != null) {
IPersist persist = flattenedSolution.searchPersist(uuid);
if (persist != null) {
parent = persist.getParent();
while (!(parent instanceof Form)) {
parentsOfo1.add(parent);
parent = parent.getParent();
}
// also add the form to the list of parents
parentsOfo1.add(parent);
}
}
}
}
IPersist childo2 = (IPersist) o2;
IPersist parento2 = ((IPersist) o2).getParent();
while (!(parento2 instanceof Form)) {
if (parentsOfo1.contains(parento2)) {
int index = parentsOfo1.indexOf(parento2);
// delegate to Yx
if (index > 0) {
yxCompare = PositionComparator.YX_PERSIST_COMPARATOR.compare(parentsOfo1.get(index - 1), childo2);
}
}
childo2 = parento2;
parento2 = parento2.getParent();
}
// also check to see if the common parent is the actual form
if (parentsOfo1.contains(parento2)) {
int index = parentsOfo1.indexOf(parento2);
if (index > 0) {
yxCompare = PositionComparator.YX_PERSIST_COMPARATOR.compare(parentsOfo1.get(index - 1), childo2);
}
}
if (parento2 instanceof Form && !parento2.equals(form)) {
if (((Form) parento2).isFormComponent() && o2 instanceof AbstractBase) {
String uuid = ((AbstractBase) o2).getRuntimeProperty(FORM_COMPONENT_UUID);
if (uuid != null) {
IPersist persist = flattenedSolution.searchPersist(uuid);
if (persist != null) {
parento2 = persist.getParent();
while (!(parento2 instanceof Form)) {
if (parentsOfo1.contains(parento2)) {
int index = parentsOfo1.indexOf(parento2);
// delegate to Yx
if (index > 0) {
yxCompare = PositionComparator.YX_PERSIST_COMPARATOR.compare(parentsOfo1.get(index - 1), childo2);
}
}
childo2 = parento2;
parento2 = parento2.getParent();
}
}
}
}
}
if (yxCompare == 0 && parentsOfo1.contains(parento2) && parentsOfo1.size() > 1) {
yxCompare = PositionComparator.YX_PERSIST_COMPARATOR.compare(parentsOfo1.get(parentsOfo1.size() - 2), childo2);
}
}
} else if (form instanceof Form && !((Form) form).isResponsiveLayout()) {
yxCompare = PositionComparator.YX_PERSIST_COMPARATOR.compare((IPersist) o1, (IPersist) o2);
}
// if they are at the same position, and are different persist, just use UUID to decide the sequence
return yxCompare == 0 ? ((IPersist) o1).getUUID().compareTo(((IPersist) o2).getUUID()) : yxCompare;
} else if (seq1 == ISupportTabSeq.DEFAULT) {
return 1;
} else if (seq2 == ISupportTabSeq.DEFAULT) {
return -1;
}
return seq1 - seq2;
}
use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.
the class FormElementHelper method getControlledTabSeqReplacementFor.
/**
* Generates a Servoy controlled tab-sequence-index. We try to avoid sending default (0 or null) tabSeq even
* for forms that do use default tab sequence in order to avoid problems with nesting default and non-default tabSeq forms.
*
* @param designValue the value the persist holds for the tabSeq.
* @param form the form containing the persist
* @param persistIfAvailable the persist. For now, 'component' type properties might work with non-persist-linked FormElements so it could be null.
* When those become persist based as well this will never be null.
* @return the requested controlled tabSeq (should make tabSeq be identical to the one shown in developer).
*/
public Integer getControlledTabSeqReplacementFor(Integer designValue, PropertyDescription pd, Form flattenedForm, IPersist persistIfAvailable, // TODO more args will be needed here such as the tabSeq property name or description
FlattenedSolution flattenedSolution, // TODO more args will be needed here such as the tabSeq property name or description
boolean design) {
// TODO this can be removed when we know we'll always have a persist here; currently don't handle this in any way as it's not supported
if (persistIfAvailable == null || (nestedCall.get() != null && nestedCall.get().booleanValue()))
return designValue;
nestedCall.set(Boolean.TRUE);
final boolean responsiveForm = flattenedForm.isResponsiveLayout();
try {
if (flattenedForm.isFormComponent() && persistIfAvailable instanceof AbstractBase) {
String mainFormName = ((AbstractBase) persistIfAvailable).getRuntimeProperty(FORM_COMPONENT_FORM_NAME);
if (mainFormName != null) {
flattenedForm = flattenedSolution.getFlattenedForm(flattenedSolution.getForm(mainFormName));
// just return the skip value if this is somehow invalid.
if (flattenedForm == null)
return Integer.valueOf(-2);
}
}
boolean formWasModifiedViaSolutionModel = flattenedSolution.hasCopy(flattenedForm);
Map<TabSeqProperty, Integer> cachedTabSeq = null;
if (formWasModifiedViaSolutionModel) {
Pair<Long, Map<TabSeqProperty, Integer>> pair = flattenedForm.getRuntimeProperty(FORM_TAB_SEQUENCE);
if (pair != null && flattenedForm.getLastModified() == pair.getLeft().longValue()) {
cachedTabSeq = pair.getRight();
}
} else
cachedTabSeq = formTabSequences.get(flattenedForm.getUUID());
if (cachedTabSeq == null) {
cachedTabSeq = new HashMap<TabSeqProperty, Integer>();
SortedList<TabSeqProperty> selected = new SortedList<TabSeqProperty>(new Comparator<TabSeqProperty>() {
public int compare(TabSeqProperty o1, TabSeqProperty o2) {
int seq1 = o1.getSeqValue();
int seq2 = o2.getSeqValue();
if (seq1 == ISupportTabSeq.DEFAULT && seq2 == ISupportTabSeq.DEFAULT) {
if (responsiveForm) {
if (o1.element.getParent() == o2.element.getParent()) {
int positionComparator = PositionComparator.comparePoint(false, o1.getLocation(), o2.getLocation());
if (positionComparator != 0) {
return positionComparator;
}
} else {
List<ISupportChilds> ancestors = new ArrayList<ISupportChilds>();
IPersist persist = o1.element;
while (persist.getParent() instanceof AbstractContainer) {
ancestors.add(persist.getParent());
persist = persist.getParent();
}
persist = o2.element;
while (persist.getParent() instanceof AbstractContainer) {
if (ancestors.contains(persist.getParent())) {
// we found the common ancestor
int index = ancestors.indexOf(persist.getParent());
IPersist comparablePersist = index == 0 ? o1.element : ancestors.get(index - 1);
int positionComparator = PositionComparator.comparePoint(false, ((ISupportBounds) comparablePersist).getLocation(), ((ISupportBounds) persist).getLocation());
if (positionComparator != 0) {
return positionComparator;
}
}
persist = persist.getParent();
}
}
} else {
int positionComparator = PositionComparator.comparePoint(false, o1.getLocation(), o2.getLocation());
if (positionComparator != 0) {
return positionComparator;
}
}
}
return compareTabSeq(seq1, o1.element, seq2, o2.element, flattenedSolution);
}
});
Map<TabSeqProperty, List<TabSeqProperty>> listFormComponentMap = new HashMap<TabSeqProperty, List<TabSeqProperty>>();
List<TabSeqProperty> listFormComponentElements = null;
TabSeqProperty listFormComponentTabSeq = null;
Iterator<IFormElement> iterator = flattenedForm.getFlattenedObjects(null).iterator();
while (iterator.hasNext()) {
IFormElement formElement = iterator.next();
if (FormTemplateGenerator.isWebcomponentBean(formElement)) {
String componentType = FormTemplateGenerator.getComponentTypeName(formElement);
WebObjectSpecification specification = WebComponentSpecProvider.getSpecProviderState().getWebComponentSpecification(componentType);
if (specification != null) {
Collection<PropertyDescription> properties = specification.getProperties(NGTabSeqPropertyType.NG_INSTANCE);
Collection<PropertyDescription> formComponentProperties = specification.getProperties(FormComponentPropertyType.INSTANCE);
boolean isListFormComponent = isListFormComponent(formComponentProperties);
if (properties != null && properties.size() > 0) {
IBasicWebComponent webComponent = (IBasicWebComponent) formElement;
for (PropertyDescription tabSeqProperty : properties) {
int tabseq = Utils.getAsInteger(webComponent.getProperty(tabSeqProperty.getName()));
TabSeqProperty seqProperty = new TabSeqProperty(formElement, tabSeqProperty.getName());
if (listFormComponentTabSeq == null && isListFormComponent) {
listFormComponentTabSeq = seqProperty;
listFormComponentElements = new ArrayList<TabSeqProperty>();
listFormComponentMap.put(listFormComponentTabSeq, listFormComponentElements);
}
if (tabseq >= 0) {
selected.add(seqProperty);
} else {
cachedTabSeq.put(seqProperty, Integer.valueOf(-2));
}
}
}
addFormComponentProperties(formComponentProperties, formElement, flattenedSolution, cachedTabSeq, selected, listFormComponentElements, design, new HashSet<String>());
}
} else if (formElement instanceof ISupportTabSeq) {
if (((ISupportTabSeq) formElement).getTabSeq() >= 0) {
selected.add(new TabSeqProperty(formElement, StaticContentSpecLoader.PROPERTY_TABSEQ.getPropertyName()));
} else {
cachedTabSeq.put(new TabSeqProperty(formElement, StaticContentSpecLoader.PROPERTY_TABSEQ.getPropertyName()), Integer.valueOf(-2));
}
}
}
int i = 1;
for (TabSeqProperty tabSeq : selected) {
cachedTabSeq.put(tabSeq, Integer.valueOf(i++));
}
for (TabSeqProperty tabSeq : listFormComponentMap.keySet()) {
List<TabSeqProperty> elements = listFormComponentMap.get(tabSeq);
if (elements != null) {
Integer value = cachedTabSeq.get(tabSeq);
// all elements inside list form component have same tabindex as the list itself
for (TabSeqProperty tabSeqElement : elements) {
cachedTabSeq.put(tabSeqElement, value);
}
}
}
if (!formWasModifiedViaSolutionModel) {
formTabSequences.putIfAbsent(flattenedForm.getUUID(), cachedTabSeq);
} else {
flattenedForm.setRuntimeProperty(FORM_TAB_SEQUENCE, new Pair<>(Long.valueOf(flattenedForm.getLastModified()), cachedTabSeq));
}
}
IPersist realPersist = flattenedForm.findChild(persistIfAvailable.getUUID());
if (realPersist == null) {
realPersist = persistIfAvailable;
}
Integer controlledTabSeq = cachedTabSeq.get(new TabSeqProperty(realPersist, pd.getName()));
// if not in tabSeq, use "skip" value
if (controlledTabSeq == null)
controlledTabSeq = Integer.valueOf(-2);
return controlledTabSeq;
} finally {
nestedCall.set(Boolean.FALSE);
}
}
use of com.servoy.j2db.persistence.AbstractBase in project servoy-client by Servoy.
the class FormElementHelper method generateFormComponentPersists.
private static List<IFormElement> generateFormComponentPersists(INGFormElement parent, PropertyDescription pd, JSONObject json, Form frm, FlattenedSolution fs) {
List<IFormElement> elements = new ArrayList<>();
List<IFormElement> formelements = fs.getFlattenedForm(frm).getFlattenedObjects(PositionComparator.XY_PERSIST_COMPARATOR);
for (IFormElement element : formelements) {
element = (IFormElement) ((AbstractBase) element).clonePersist(null);
// we kind of want to have this element a new uuid, but then it is very hard to get it stable.
UUID newElementUUID = null;
synchronized (formComponentElementsUUIDS) {
Map<UUID, UUID> map = formComponentElementsUUIDS.get(parent.getPersistIfAvailable().getUUID());
if (map == null) {
map = new HashMap<>();
formComponentElementsUUIDS.put(parent.getPersistIfAvailable().getUUID(), map);
}
newElementUUID = map.get(element.getUUID());
if (newElementUUID == null) {
newElementUUID = UUID.randomUUID();
map.put(element.getUUID(), newElementUUID);
}
}
((AbstractBase) element).resetUUID(newElementUUID);
String elementName = element.getName();
if (elementName == null) {
elementName = FormElement.SVY_NAME_PREFIX + String.valueOf(element.getID());
}
String templateName = getStartElementName(parent, pd) + elementName;
String formName = parent.getForm().getName();
if (parent.getForm().isFormComponent() && parent.getPersistIfAvailable() instanceof AbstractBase && ((AbstractBase) parent.getPersistIfAvailable()).getRuntimeProperty(FORM_COMPONENT_FORM_NAME) != null) {
formName = ((AbstractBase) parent.getPersistIfAvailable()).getRuntimeProperty(FORM_COMPONENT_FORM_NAME);
}
((AbstractBase) element).setRuntimeProperty(FORM_COMPONENT_FORM_NAME, formName);
((AbstractBase) element).setRuntimeProperty(FORM_COMPONENT_UUID, parent.getPersistIfAvailable().getUUID().toString());
JSONObject elementJson = json.optJSONObject(elementName);
if (elementJson != null) {
Map<String, Method> methods = RepositoryHelper.getSetters(element);
WebObjectSpecification legacySpec = FormTemplateGenerator.getWebObjectSpecification(element);
for (String key : elementJson.keySet()) {
Object val = elementJson.get(key);
if (val != null && methods.get(key) != null) {
Method method = methods.get(key);
Class<?> paramType = method.getParameterTypes()[0];
if (!paramType.isAssignableFrom(val.getClass()) && !(paramType.isPrimitive() && val instanceof Number)) {
PropertyDescription property = legacySpec.getProperty(key);
if (property != null && property.getType() instanceof IDesignValueConverter) {
val = ((IDesignValueConverter) property.getType()).fromDesignValue(val, property);
} else {
// will not fit, very likely a uuid that should be an int.
if (val != null) {
IPersist found = fs.searchPersist(val.toString());
if (found != null)
val = Integer.valueOf(found.getID());
}
}
}
}
if (val instanceof JSONObject && ((AbstractBase) element).getProperty(key) instanceof JSONObject) {
// if both are json (like a nested form) then merge it in.
ServoyJSONObject.mergeAndDeepCloneJSON((JSONObject) val, (JSONObject) ((AbstractBase) element).getProperty(key));
} else if (val instanceof String && StaticContentSpecLoader.PROPERTY_CUSTOMPROPERTIES.getPropertyName().equals(key) && ((AbstractBase) element).getCustomProperties() != null) {
// custom properties needs to be merged in..
JSONObject original = new ServoyJSONObject(((AbstractBase) element).getCustomProperties(), true);
ServoyJSONObject.mergeAndDeepCloneJSON(new ServoyJSONObject((String) val, true), original);
((AbstractBase) element).setCustomProperties(ServoyJSONObject.toString(original, true, true, true));
} else if (val instanceof JSONArray && ((AbstractBase) element).getProperty(key) instanceof IChildWebObject[]) {
IChildWebObject[] webObjectChildren = (IChildWebObject[]) ((AbstractBase) element).getProperty(key);
JSONArray original = new JSONArray();
for (IChildWebObject element2 : webObjectChildren) {
original.put(element2.getJson());
}
ServoyJSONObject.mergeAndDeepCloneJSON((JSONArray) val, original);
((AbstractBase) element).setProperty(key, original);
} else
((AbstractBase) element).setProperty(key, val);
}
}
element.setName(templateName);
elements.add(element);
}
return elements;
}
Aggregations