private String debugObjectGraph( final ObjectAdapter object, final int level, final Vector<ObjectAdapter> recursiveElements) { final StringBuffer s = new StringBuffer(); recursiveElements.addElement(object); // work through all its fields final List<ObjectAssociation> fields = object.getSpecification().getAssociations(Contributed.EXCLUDED); for (int i = 0; i < fields.size(); i++) { final ObjectAssociation field = fields.get(i); final Object obj = field.get(object); final String id = field.getId(); indent(s, level); if (field.isOneToManyAssociation()) { s.append( id + ": \n" + debugCollectionGraph((ObjectAdapter) obj, level + 1, recursiveElements)); } else { if (recursiveElements.contains(obj)) { s.append(id + ": " + obj + "*\n"); } else { s.append(id + ": " + obj); s.append(debugGraph((ObjectAdapter) obj, level + 1, recursiveElements)); } } } return s.toString(); }
private static void simpleObject( final ObjectAdapter collectionAdapter, final DebugBuilder debugBuilder) { debugBuilder.appendln(collectionAdapter.titleString()); final ObjectSpecification objectSpec = collectionAdapter.getSpecification(); if (objectSpec.isParentedOrFreeCollection()) { final CollectionFacet facet = CollectionFacetUtils.getCollectionFacetFromSpec(collectionAdapter); int i = 1; for (final ObjectAdapter element : facet.collection(collectionAdapter)) { debugBuilder.appendln(i++ + " " + element.titleString()); } } else { // object is a regular Object try { final List<ObjectAssociation> fields = objectSpec.getAssociations(Contributed.EXCLUDED); for (int i = 0; i < fields.size(); i++) { final ObjectAssociation field = fields.get(i); final ObjectAdapter obj = field.get(collectionAdapter); final String name = field.getId(); if (obj == null) { debugBuilder.appendln(name, "null"); } else { debugBuilder.appendln(name, obj.titleString()); } } } catch (final RuntimeException e) { debugBuilder.appendException(e); } } }
private ObjectAssociation getAssociationWithId(final String id) { for (final ObjectAssociation objectAssociation : getAssociations(Contributed.INCLUDED)) { if (objectAssociation.getId().equals(id)) { return objectAssociation; } } return null; }
private void cataloguePropertiesAndCollections(final Map<Method, ObjectMember> membersByMethod) { final Filter<ObjectAssociation> noop = Filters.anyOfType(ObjectAssociation.class); final List<ObjectAssociation> fields = getAssociations(noop); for (int i = 0; i < fields.size(); i++) { final ObjectAssociation field = fields.get(i); final List<Facet> facets = field.getFacets(ImperativeFacet.FILTER); for (final Facet facet : facets) { final ImperativeFacet imperativeFacet = ImperativeFacetUtils.getImperativeFacet(facet); for (final Method imperativeFacetMethod : imperativeFacet.getMethods()) { membersByMethod.put(imperativeFacetMethod, field); } } } }
public void determineMaximum(final Content content) { int i = 0; for (final ObjectAssociation field : fields) { maxValues[i] = 0; final CollectionFacet collectionFacet = content.getAdapter().getSpecification().getFacet(CollectionFacet.class); for (final ObjectAdapter element : collectionFacet.iterable(content.getAdapter())) { final ObjectAdapter value = field.get(element); final double doubleValue = NumberAdapters.doubleValue(field, value); maxValues[i] = Math.max(maxValues[i], doubleValue); } i++; } }
private void ensureServiceHasNoAssociations() { final List<ObjectAssociation> associations = getAssociations(); final StringBuilder buf = new StringBuilder(); for (final ObjectAssociation association : associations) { final String name = association.getId(); // services are allowed to have one association, called 'id' if (!isValidAssociationForService(name)) { appendAssociationName(buf, name); } } if (buf.length() > 0) { throw new ObjectSpecificationException( "Service object " + getFullIdentifier() + " should have no fields, but has: " + buf); } }
private static void objectGraph( final ObjectAdapter adapter, final int level, final List<ObjectAdapter> ignoreAdapters, final DebugBuilder s, final AuthenticationSession authenticationSession) { ignoreAdapters.add(adapter); try { // work through all its fields final List<ObjectAssociation> fields = adapter.getSpecification().getAssociations(Contributed.EXCLUDED); for (int i = 0; i < fields.size(); i++) { final ObjectAssociation field = fields.get(i); final ObjectAdapter obj = field.get(adapter); final String name = field.getId(); graphIndent(level, s); if (field.isVisible(authenticationSession, adapter, where).isVetoed()) { s.append(name + ": (not visible)"); s.append("\n"); } else { if (obj == null) { s.append(name + ": null\n"); /* * } else if (obj.getSpecification().isParseable()) { * s.append(name + ": " + obj.titleString()); * s.append("\n"); */ } else { if (ignoreAdapters.contains(obj)) { s.append(name + ": " + obj + "*\n"); } else { s.append(name + ": " + obj); graph(obj, level + 1, ignoreAdapters, authenticationSession, s); } } } } } catch (final RuntimeException e) { s.appendException(e); } }
/** Walking the graph. */ public void resolveField(final ObjectAdapter object, final ObjectAssociation association) { ensureOpened(); ensureInTransaction(); final ObjectAdapter referencedCollectionAdapter = association.get(object); // this code originally brought in from the JPA impl, but seems reasonable. if (association.isOneToManyAssociation()) { ensureThatState(referencedCollectionAdapter, is(notNullValue())); final Object referencedCollection = referencedCollectionAdapter.getObject(); ensureThatState(referencedCollection, is(notNullValue())); // if a proxy collection, then force it to initialize. just 'touching' the object is // sufficient. // REVIEW: I wonder if this is actually needed; does JDO use proxy collections? referencedCollection.hashCode(); } // the JPA impl used to also call its lifecycle listener on the referenced collection object, eg // List, // itself. I don't think this makes sense to do for JDO (the collection is not a // PersistenceCapable). }
private View createFieldView( final View view, final Axes axes, final ObjectAdapter object, final int fieldNumber, final ObjectAssociation field) { if (field == null) { throw new NullPointerException(); } if (field.isOneToOneAssociation()) { IsisContext.getPersistenceSession().resolveField(object, field); } final Content content1 = Toolkit.getContentFactory().createFieldContent(field, object); final View fieldView = subviewDesign.createView(content1, axes, fieldNumber); return fieldView; }
@Override public void process(final Request request) { final TableBlock tableBlock = (TableBlock) request.getBlockContent(); final String id = request.getOptionalProperty(OBJECT); final String fieldName = request.getRequiredProperty(FIELD); final String linkView = request.getOptionalProperty(LINK_VIEW); String className = request.getOptionalProperty(CLASS); className = className == null ? "" : " class=\"" + className + "\""; RequestContext context = request.getContext(); final ObjectAdapter object = context.getMappedObjectOrVariable(id, tableBlock.getElementName()); final ObjectAssociation field = object.getSpecification().getAssociation(fieldName); if (field == null) { throw new ScimpiException( "No field " + fieldName + " in " + object.getSpecification().getFullIdentifier()); } request.appendHtml("<td" + className + ">"); if (field.isVisible(IsisContext.getAuthenticationSession(), object, where).isAllowed()) { final ObjectAdapter fieldReference = field.get(object); final String source = fieldReference == null ? "" : context.mapObject(fieldReference, Scope.REQUEST); final String name = request.getOptionalProperty(RESULT_NAME, fieldName); context.addVariable(name, Request.getEncoder().encoder(source), Scope.REQUEST); if (linkView != null) { final String linkId = context.mapObject(object, Scope.REQUEST); final String linkName = request.getOptionalProperty(LINK_NAME, RequestContext.RESULT); final String linkObject = request.getOptionalProperty(LINK_OBJECT, linkId); request.appendHtml( "<a href=\"" + linkView + "?" + linkName + "=" + linkObject + context.encodedInteractionParameters() + "\">"); } else if (tableBlock.getlinkView() != null) { String linkObjectInVariable = tableBlock.getElementName(); final String linkId = (String) context.getVariable(linkObjectInVariable); request.appendHtml( "<a href=\"" + tableBlock.getlinkView() + "?" + tableBlock.getlinkName() + "=" + linkId + context.encodedInteractionParameters() + "\">"); } request.pushNewBuffer(); request.processUtilCloseTag(); final String buffer = request.popBuffer(); if (buffer.trim().length() == 0) { request.appendAsHtmlEncoded(fieldReference == null ? "" : fieldReference.titleString()); } else { request.appendHtml(buffer); } if (linkView != null) { request.appendHtml("</a>"); } } else { request.skipUntilClose(); } request.appendHtml("</td>"); }
@Override public void process(final RequestContext context) throws IOException { AuthenticationSession session = context.getSession(); if (session == null) { session = new AnonymousSession(); } try { final String objectId = context.getParameter("_" + OBJECT); final String version = context.getParameter("_" + VERSION); final String formId = context.getParameter("_" + FORM_ID); String resultName = context.getParameter("_" + RESULT_NAME); resultName = resultName == null ? RequestContext.RESULT : resultName; final String override = context.getParameter("_" + RESULT_OVERRIDE); String message = context.getParameter("_" + MESSAGE); final ObjectAdapter adapter = context.getMappedObject(objectId); final List<ObjectAssociation> fields = adapter .getSpecification() .getAssociations( Contributed.EXCLUDED, ObjectAssociation.Filters.dynamicallyVisible(session, adapter, where)); for (final ObjectAssociation objectAssociation : fields) { if (objectAssociation.isVisible(session, adapter, where).isVetoed()) { throw new NotLoggedInException(); } } final FormState entryState = validateObject(context, adapter, fields); final Version adapterVersion = adapter.getVersion(); final Version formVersion = context.getVersion(version); if (formVersion != null && adapterVersion.different(formVersion)) { IsisContext.getMessageBroker() .addMessage( "The " + adapter.getSpecification().getSingularName() + " was edited " + "by another user (" + adapterVersion.getUser() + "). Please make your changes based on their changes."); final String view = context.getParameter("_" + ERROR); context.setRequestPath(view, Dispatcher.EDIT); entryState.setForm(formId); context.addVariable(ENTRY_FIELDS, entryState, Scope.REQUEST); context.addVariable(resultName, objectId, Scope.REQUEST); if (override != null) { context.addVariable(resultName, override, Scope.REQUEST); } } else if (entryState.isValid()) { changeObject(context, adapter, entryState, fields); if (adapter.isTransient()) { IsisContext.getPersistenceSession().makePersistent(adapter); context.unmapObject(adapter, Scope.REQUEST); } String view = context.getParameter("_" + VIEW); final String id = context.mapObject(adapter, Scope.REQUEST); context.addVariable(resultName, id, Scope.REQUEST); if (override != null) { context.addVariable(resultName, override, Scope.REQUEST); } final int questionMark = view == null ? -1 : view.indexOf("?"); if (questionMark > -1) { final String params = view.substring(questionMark + 1); final int equals = params.indexOf("="); context.addVariable( params.substring(0, equals), params.substring(equals + 1), Scope.REQUEST); view = view.substring(0, questionMark); } context.setRequestPath(view); if (message == null) { message = "Saved changes to " + adapter.getSpecification().getSingularName(); } else if (message.equals("")) { message = null; } if (message != null) { final MessageBroker messageBroker = IsisContext.getMessageBroker(); messageBroker.addMessage(message); } } else { final String view = context.getParameter("_" + ERROR); context.setRequestPath(view, Dispatcher.EDIT); entryState.setForm(formId); context.addVariable(ENTRY_FIELDS, entryState, Scope.REQUEST); context.addVariable(resultName, objectId, Scope.REQUEST); if (override != null) { context.addVariable(resultName, override, Scope.REQUEST); } final MessageBroker messageBroker = IsisContext.getMessageBroker(); messageBroker.addWarning(entryState.getError()); } } catch (final RuntimeException e) { IsisContext.getMessageBroker().getMessages(); IsisContext.getMessageBroker().getWarnings(); throw e; } }
private FormState validateObject( final RequestContext context, final ObjectAdapter object, final List<ObjectAssociation> fields) { final FormState formState = new FormState(); for (int i = 0; i < fields.size(); i++) { final ObjectAssociation field = fields.get(i); final String fieldId = field.getId(); String newEntry = context.getParameter(fieldId); if (fields.get(i).isOneToManyAssociation()) { continue; } if (fields .get(i) .isVisible(IsisContext.getAuthenticationSession(), object, where) .isVetoed()) { continue; } if (field.isUsable(IsisContext.getAuthenticationSession(), object, where).isVetoed()) { continue; } if (newEntry != null && newEntry.equals("-OTHER-")) { newEntry = context.getParameter(fieldId + "-other"); } if (newEntry == null) { // TODO duplicated in EditObject; line 97 final ObjectSpecification spec = field.getSpecification(); if (spec.isOfType(IsisContext.getSpecificationLoader().loadSpecification(boolean.class)) || spec.isOfType( IsisContext.getSpecificationLoader().loadSpecification(Boolean.class))) { newEntry = FALSE; } else { continue; } } final FieldEditState fieldState = formState.createField(fieldId, newEntry); Consent consent = null; if (field.isMandatory() && (newEntry.equals("") || newEntry.equals("NULL"))) { consent = new Veto(field.getName() + " required"); formState.setError("Not all fields have been set"); } else if (field.getSpecification().containsFacet(ParseableFacet.class)) { try { final ParseableFacet facet = field.getSpecification().getFacet(ParseableFacet.class); final ObjectAdapter originalValue = field.get(object); Localization localization = IsisContext.getLocalization(); final ObjectAdapter newValue = facet.parseTextEntry(originalValue, newEntry, localization); consent = ((OneToOneAssociation) field).isAssociationValid(object, newValue); fieldState.setValue(newValue); } catch (final TextEntryParseException e) { consent = new Veto(e.getMessage()); // formState.setError("Not all fields have been entered correctly"); } } else { final ObjectAdapter associate = newEntry.equals("null") ? null : context.getMappedObject(newEntry); if (associate != null) { IsisContext.getPersistenceSession().resolveImmediately(associate); } consent = ((OneToOneAssociation) field).isAssociationValid(object, associate); fieldState.setValue(associate); } if (consent.isVetoed()) { fieldState.setError(consent.getReason()); formState.setError("Not all fields have been entered correctly"); } } // TODO check the state of the complete object. return formState; }
private static void specificationFields( final ObjectSpecification specification, final DebugBuilder debugBuilder) { final List<ObjectAssociation> fields = specification.getAssociations(Contributed.EXCLUDED); debugBuilder.appendln("All"); debugBuilder.indent(); for (int i = 0; i < fields.size(); i++) { debugBuilder.appendln((i + 1) + "." + fields.get(i).getId()); } debugBuilder.unindent(); final List<ObjectAssociation> fields2 = specification.getAssociations( Contributed.EXCLUDED, ObjectAssociation.Filters.VISIBLE_AT_LEAST_SOMETIMES); debugBuilder.appendln("Static"); debugBuilder.indent(); for (int i = 0; i < fields2.size(); i++) { debugBuilder.appendln((i + 1) + "." + fields2.get(i).getId()); } debugBuilder.unindent(); debugBuilder.appendln(); try { if (fields.size() == 0) { debugBuilder.appendln("none"); } else { for (int i = 0; i < fields.size(); i++) { final ObjectAssociation field = fields.get(i); debugBuilder.appendln( (i + 1) + "." + field.getId() + " (" + field.getClass().getName() + ")"); debugBuilder.indent(); final String description = field.getDescription(); if (description != null && !description.equals("")) { debugBuilder.appendln("Description", description); } final String help = field.getHelp(); if (help != null && !help.equals("")) { debugBuilder.appendln( "Help", help.substring(0, Math.min(30, help.length())) + (help.length() > 30 ? "..." : "")); } debugBuilder.appendln("ID", field.getIdentifier()); debugBuilder.appendln("Short ID", field.getId()); debugBuilder.appendln("Name", field.getName()); final String type = field.isOneToManyAssociation() ? "Collection" : field.isOneToOneAssociation() ? "Object" : "Unknown"; debugBuilder.appendln("Type", type); final ObjectSpecification fieldSpec = field.getSpecification(); final boolean hasIdentity = !(fieldSpec.isParentedOrFreeCollection() || fieldSpec.isParented() || fieldSpec.isValue()); debugBuilder.appendln("Has identity", hasIdentity); debugBuilder.appendln("Spec", fieldSpec.getFullIdentifier()); debugBuilder.appendln( "Flags", (field.isAlwaysHidden() ? "" : "Visible ") + (field.isNotPersisted() ? "Not Persisted " : " ") + (field.isMandatory() ? "Mandatory " : "")); final Class<? extends Facet>[] facets = field.getFacetTypes(); if (facets.length > 0) { debugBuilder.appendln("Facets"); debugBuilder.indent(); boolean none = true; for (final Class<? extends Facet> facet : facets) { debugBuilder.appendln(field.getFacet(facet).toString()); none = false; } if (none) { debugBuilder.appendln("none"); } debugBuilder.unindent(); } debugBuilder.appendln(field.debugData()); debugBuilder.unindent(); } } } catch (final RuntimeException e) { debugBuilder.appendException(e); } }
private void updateBuild( final View view, final Axes axes, final ObjectAdapter object, final List<ObjectAssociation> flds) { LOG.debug(" as update build"); /* * 1/ To remove fields: look through views and remove any that don't * exists in visible fields * * 2/ From remaining views, check for changes as already being done, and * replace if needed * * 3/ Finally look through fields to see if there is no existing * subview; and add one */ View[] subviews = view.getSubviews(); // remove views for fields that no longer exist outer: for (int i = 0; i < subviews.length; i++) { final FieldContent fieldContent = ((FieldContent) subviews[i].getContent()); for (int j = 0; j < flds.size(); j++) { final ObjectAssociation field = flds.get(j); if (fieldContent.getField() == field) { continue outer; } } view.removeView(subviews[i]); } // update existing fields if needed subviews = view.getSubviews(); for (int i = 0; i < subviews.length; i++) { final View subview = subviews[i]; final ObjectAssociation field = ((FieldContent) subview.getContent()).getField(); final ObjectAdapter value = field.get(object); if (field.isOneToManyAssociation()) { subview.update(value); } else if (field.isOneToOneAssociation()) { final ObjectAdapter existing = subview.getContent().getAdapter(); // if the field is parseable then it may have been modified; we // need to replace what was // typed in with the actual title. if (!field.getSpecification().isParseable()) { final boolean changedValue = value != existing; final boolean isDestroyed = existing != null && existing.isDestroyed(); if (changedValue || isDestroyed) { View fieldView; fieldView = createFieldView(view, axes, object, i, field); if (fieldView != null) { view.replaceView(subview, decorateSubview(axes, fieldView)); } else { view.addView(new FieldErrorView("No field for " + value)); } } } else { if (AdapterUtils.exists(value) && !AdapterUtils.wrappedEqual(value, existing)) { final View fieldView = createFieldView(view, axes, object, i, field); view.replaceView(subview, decorateSubview(axes, fieldView)); } else { subview.refresh(); } } } else { throw new UnknownTypeException(field.getName()); } } // add new fields outer2: for (int j = 0; j < flds.size(); j++) { final ObjectAssociation field = flds.get(j); for (int i = 0; i < subviews.length; i++) { final FieldContent fieldContent = ((FieldContent) subviews[i].getContent()); if (fieldContent.getField() == field) { continue outer2; } } addField(view, axes, object, field, j); } }
@Override public void resolveField(final ObjectAdapter object, final ObjectAssociation field) throws ObjectPersistenceException { final ObjectAdapter referenceAdapter = field.get(object); referenceAdapter.markAsResolvedIfPossible(); }