private void addValue() { final ObjectAdapter valueAdapter = objectMember.get(objectAdapter); // use the runtime type if we have a value, else the compile time type of the member otherwise final ObjectSpecification spec = valueAdapter != null ? valueAdapter.getSpecification() : objectMember.getSpecification(); final ValueFacet valueFacet = spec.getFacet(ValueFacet.class); if (valueFacet != null) { String format = null; final Class<?> specClass = spec.getCorrespondingClass(); if (specClass == java.math.BigDecimal.class) { // look for facet on member, else on the value's spec final BigDecimalValueFacet bigDecimalValueFacet = getFacet( BigDecimalValueFacet.class, objectMember, valueAdapter != null ? valueAdapter.getSpecification() : null); if (bigDecimalValueFacet != null) { final Integer precision = bigDecimalValueFacet.getPrecision(); final Integer scale = bigDecimalValueFacet.getScale(); format = String.format("big-decimal(%d,%d)", precision, scale); } } else if (specClass == java.math.BigInteger.class) { // TODO: need to extend BigIntegerValueFacet similar to BigDecimalValueFacet } JsonValueEncoder.appendValueAndFormat(spec, valueAdapter, representation, format); return; } final RenderFacet renderFacet = objectMember.getFacet(RenderFacet.class); boolean eagerlyRender = renderFacet != null && renderFacet.value() == Type.EAGERLY && rendererContext.canEagerlyRender(valueAdapter); if (valueAdapter == null) { representation.mapPut("value", NullNode.getInstance()); } else { final TitleFacet titleFacet = spec.getFacet(TitleFacet.class); final String title = titleFacet.title(valueAdapter, rendererContext.getLocalization()); final LinkBuilder valueLinkBuilder = DomainObjectReprRenderer.newLinkToBuilder(rendererContext, Rel.VALUE, valueAdapter) .withTitle(title); if (eagerlyRender) { final DomainObjectReprRenderer renderer = new DomainObjectReprRenderer( rendererContext, getLinkFollowSpecs(), JsonRepresentation.newMap()); renderer.with(valueAdapter); if (mode.isEventSerialization()) { renderer.asEventSerialization(); } valueLinkBuilder.withValue(renderer.render()); } representation.mapPut("value", valueLinkBuilder.build()); } }
void appendServicePathsAndDefinitions() { // take copy to avoid concurrent modification exception final Collection<ObjectSpecification> allSpecs = Lists.newArrayList(specificationLoader.allSpecifications()); for (final ObjectSpecification serviceSpec : allSpecs) { final DomainServiceFacet domainServiceFacet = serviceSpec.getFacet(DomainServiceFacet.class); if (domainServiceFacet == null) { continue; } if (visibility.isPublic() && domainServiceFacet.getNatureOfService() != NatureOfService.VIEW_REST_ONLY) { continue; } if (domainServiceFacet.getNatureOfService() != NatureOfService.VIEW_MENU_ONLY && domainServiceFacet.getNatureOfService() != NatureOfService.VIEW && domainServiceFacet.getNatureOfService() != NatureOfService.VIEW_REST_ONLY) { continue; } final List<ObjectAction> serviceActions = Util.actionsOf(serviceSpec, visibility, classExcluder); if (serviceActions.isEmpty()) { continue; } appendServicePath(serviceSpec); for (final ObjectAction serviceAction : serviceActions) { appendServiceActionInvokePath(serviceSpec, serviceAction); } } }
private void addContributeeActionsIfAny( final ObjectAdapter serviceAdapter, final List<ObjectAction> contributeeActionsToAppendTo) { final ObjectSpecification specification = serviceAdapter.getSpecification(); if (specification == this) { return; } final List<ObjectAction> contributeeActions = Lists.newArrayList(); final List<ObjectAction> serviceActions = specification.getObjectActions( ActionType.ALL, Contributed.INCLUDED, Filters.<ObjectAction>any()); for (final ObjectAction serviceAction : serviceActions) { if (isAlwaysHidden(serviceAction)) { continue; } final NotContributedFacet notContributed = serviceAction.getFacet(NotContributedFacet.class); if (notContributed != null && notContributed.toActions()) { continue; } if (!(serviceAction instanceof ObjectActionImpl)) { continue; } final ObjectActionImpl contributedAction = (ObjectActionImpl) serviceAction; // see if qualifies by inspecting all parameters final int contributeeParam = contributeeParameterMatchOf(contributedAction); if (contributeeParam != -1) { ObjectActionContributee contributeeAction = new ObjectActionContributee( serviceAdapter, contributedAction, contributeeParam, this, objectMemberContext); facetProcessor.processMemberOrder(metadataProperties, contributeeAction); contributeeActions.add(contributeeAction); } } contributeeActionsToAppendTo.addAll(contributeeActions); }
private Property modelFor(final ObjectSpecification specification) { if (specification == null) { return new ObjectProperty(); } // no "simple" representation for void or values final Class<?> correspondingClass = specification.getCorrespondingClass(); if (correspondingClass == void.class || correspondingClass == Void.class) { return new ObjectProperty(); } // no "simple" representation for values final Property property = valuePropertyFactory.newProperty(correspondingClass); if (property != null) { // was recognized as a value return new ObjectProperty(); } if (specification.isParentedOrFreeCollection()) { TypeOfFacet typeOfFacet = specification.getFacet(TypeOfFacet.class); if (typeOfFacet != null) { ObjectSpecification elementSpec = typeOfFacet.valueSpec(); if (elementSpec != null) { return arrayPropertyOf(elementSpec); } } } if (specification.getCorrespondingClass() == java.lang.Object.class) { return new ObjectProperty(); } if (specification.getCorrespondingClass() == java.lang.Enum.class) { return new StringProperty(); } return newRefProperty(objectTypeFor(specification) + "Repr"); }
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); } } }
@Override protected ApplicationAdvice appliesTo(IModel<?> model) { if (model instanceof ScalarModel) { ScalarModel mo = (ScalarModel) model; ObjectSpecification os = mo.getTypeOfSpecification(); ObjectSpecification locatableSpec = getSpecificationLoader().loadSpecification(ReportPrinter.class); if (os.isOfType(locatableSpec)) { System.out.println(locatableSpec); return ApplicationAdvice.APPLIES; } /* Remove Object obj = model.getObject(); PropertyMemento pm = ((ScalarModel) model).getPropertyMemento(); if (pm != null && pm.getIdentifier().equals("description")){ //return ApplicationAdvice.APPLIES_EXCLUSIVELY; } if (obj != null) { if (obj instanceof PojoAdapter) { PojoAdapter po = (PojoAdapter) obj; ObjectSpecification os2 = po.getSpecification(); System.out.println(os2.getSingularName()); System.out.println(po.getSpecification().getCorrespondingClass().getName()); if (po.getSpecification().getCorrespondingClass().isAnnotationPresent(Html.class)) { //return ApplicationAdvice.APPLIES; } } } */ } return ApplicationAdvice.DOES_NOT_APPLY; }
private ObjectAdapterMemento(final ObjectAdapter adapter) { if (adapter == null) { throw new IllegalArgumentException("adapter cannot be null"); } final ObjectSpecification specification = adapter.getSpecification(); objectSpecId = specification.getSpecId(); init(adapter); }
@Override ObjectAdapter recreateAdapter( final ObjectAdapterMemento oam, ConcurrencyChecking concurrencyChecking) { ObjectSpecId objectSpecId = oam.objectSpecId; ObjectSpecification objectSpec = SpecUtils.getSpecificationFor(objectSpecId); final EncodableFacet encodableFacet = objectSpec.getFacet(EncodableFacet.class); return encodableFacet.fromEncodedString(oam.encodableValue); }
public static void addTargetBlankIfActionReturnsUrl( final AbstractLink link, final ObjectAction action) { final ObjectSpecification returnType = action.getReturnType(); if (returnType != null && "java.net.URL".equals(returnType.getFullIdentifier())) { link.add(new AttributeAppender("target", Model.of("_blank"))); link.add(new CssClassAppender("noVeil")); } }
private static void actionDetails( final ObjectAction objectAction, final int indent, final int count, final DebugBuilder debugBuilder) { debugBuilder.appendln( (count + 1) + "." + objectAction.getId() + " (" + objectAction.getClass().getName() + ")"); debugBuilder.indent(); try { if (objectAction.getDescription() != null && !objectAction.getDescription().equals("")) { debugBuilder.appendln("Description", objectAction.getDescription()); } debugBuilder.appendln("ID", objectAction.getId()); debugBuilder.appendln(objectAction.debugData()); debugBuilder.appendln("On type", objectAction.getOnType()); final Class<? extends Facet>[] facets = objectAction.getFacetTypes(); if (facets.length > 0) { debugBuilder.appendln("Facets"); debugBuilder.indent(); for (final Class<? extends Facet> facet : facets) { debugBuilder.appendln(objectAction.getFacet(facet).toString()); } debugBuilder.unindent(); } final ObjectSpecification returnType = objectAction.getReturnType(); debugBuilder.appendln("Returns", returnType == null ? "VOID" : returnType.toString()); final List<ObjectActionParameter> parameters = objectAction.getParameters(); if (parameters.size() == 0) { debugBuilder.appendln("Parameters", "none"); } else { debugBuilder.appendln("Parameters"); debugBuilder.indent(); final List<ObjectActionParameter> p = objectAction.getParameters(); for (int j = 0; j < parameters.size(); j++) { debugBuilder.append(p.get(j).getName()); debugBuilder.append(" ("); debugBuilder.append(parameters.get(j).getSpecification().getFullIdentifier()); debugBuilder.appendln(")"); debugBuilder.indent(); final Class<? extends Facet>[] parameterFacets = p.get(j).getFacetTypes(); for (final Class<? extends Facet> parameterFacet : parameterFacets) { debugBuilder.appendln(p.get(j).getFacet(parameterFacet).toString()); } debugBuilder.unindent(); } debugBuilder.unindent(); } } catch (final RuntimeException e) { debugBuilder.appendException(e); } debugBuilder.unindent(); }
/** API: Return specification. */ @Override public ObjectSpecification loadSpecification(final Class<?> type) { final ObjectSpecification spec = internalLoadSpecification(type); if (spec == null) { return null; } if (getCache().isInitialized() && getCache().getByObjectType(spec.getSpecId()) == null) { getCache().recache(spec.getSpecId(), spec); } return spec; }
private void cacheBySpecId() { final Map<ObjectSpecId, ObjectSpecification> specById = Maps.newHashMap(); for (final ObjectSpecification objSpec : allSpecifications()) { final ObjectSpecId objectSpecId = objSpec.getSpecId(); if (objectSpecId == null) { continue; } specById.put(objectSpecId, objSpec); } getCache().setCacheBySpecId(specById); }
@Override public void invalidateCacheFor(Object domainObject) { final Class<? extends Object> cls = domainObject.getClass(); final Class<?> substitutedType = getClassSubstitutor().getClass(cls); ObjectSpecification spec = loadSpecification(substitutedType); while (spec != null) { final Class<?> type = spec.getCorrespondingClass(); getCache().remove(type.getName()); recache(spec); spec = spec.superclass(); } }
private void findInstances( final ObjectSpecification spec, final PersistenceQueryBuiltIn persistenceQuery, final List<ObjectAdapter> foundInstances) { instancesFor(spec.getSpecId()).findInstancesAndAdd(persistenceQuery, foundInstances); // include subclasses final List<ObjectSpecification> subclasses = spec.subclasses(); for (int i = 0; i < subclasses.size(); i++) { findInstances(subclasses.get(i), persistenceQuery, foundInstances); } }
@Override protected void resetCollectionParent( final DatabaseConnector connector, final ObjectAdapter parent, final Iterator<ObjectAdapter> elements) { LOG.debug("Saving polymorphic list"); ObjectSpecification elementSpecification; while (elements.hasNext()) { final ObjectAdapter thisAdapter = elements.next(); elementSpecification = thisAdapter.getSpecification(); // Reinstall collection parent final StringBuffer update = new StringBuffer(); update.append("INSERT INTO "); update.append(table); update.append(" ("); // list of column names super.getIdMapping().appendColumnNames(update); update.append("," + getForeignKeyName()); update.append(", " + itemIdColumnName); update.append("," + classColumnName); update.append(") VALUES ("); // Row ID column final Object pojo = thisAdapter.getObject(); final RootOid transientRootOid = oidGenerator.createTransientOid(pojo); final RootOid persistentRootOid = oidGenerator.createPersistent(pojo, transientRootOid); polyIdMapper.appendObjectId(connector, update, persistentRootOid); // polyIdMapper.appendObjectId(connector, update, // thisAdapter.getOid()); update.append(","); // Foreign key ID column getForeignKeyMapping().appendInsertValues(connector, update, parent); update.append(","); // item Id column final RootOid oid = (RootOid) thisAdapter.getOid(); getIdMapping().appendObjectId(connector, update, oid); // Class name column update.append(",?)"); connector.addToQueryValues(elementSpecification.getFullIdentifier()); connector.insert(update.toString()); } }
void appendObjectPathsAndDefinitions() { // take copy to avoid concurrent modification exception final Collection<ObjectSpecification> allSpecs = Lists.newArrayList(specificationLoader.allSpecifications()); for (final ObjectSpecification objectSpec : allSpecs) { final DomainServiceFacet domainServiceFacet = objectSpec.getFacet(DomainServiceFacet.class); if (domainServiceFacet != null) { continue; } final MixinFacet mixinFacet = objectSpec.getFacet(MixinFacet.class); if (mixinFacet != null) { continue; } if (visibility.isPublic() && !Util.isVisibleForPublic(objectSpec)) { continue; } if (objectSpec.isAbstract()) { continue; } if (objectSpec.isValue()) { continue; } // special cases if (classExcluder.exclude(objectSpec)) { continue; } final List<OneToOneAssociation> objectProperties = Util.propertiesOf(objectSpec, visibility); final List<OneToManyAssociation> objectCollections = Util.collectionsOf(objectSpec, visibility); final List<ObjectAction> objectActions = Util.actionsOf(objectSpec, visibility, classExcluder); if (objectProperties.isEmpty() && objectCollections.isEmpty()) { continue; } final ModelImpl isisModel = appendObjectPathAndModelDefinitions(objectSpec); updateObjectModel(isisModel, objectSpec, objectProperties, objectCollections); for (final OneToManyAssociation objectCollection : objectCollections) { appendCollectionTo(objectSpec, objectCollection); } for (final ObjectAction objectAction : objectActions) { appendObjectActionInvokePath(objectSpec, objectAction); } } }
/** * Determines if this class represents the same class, or a subclass, of the specified class. * * <p>cf {@link Class#isAssignableFrom(Class)}, though target and parameter are the opposite way * around, ie: * * <pre> * cls1.isAssignableFrom(cls2); * </pre> * * <p>is equivalent to: * * <pre> * spec2.isOfType(spec1); * </pre> * * <p>Callable after {@link #introspectTypeHierarchyAndMembers()} has been called. */ @Override public boolean isOfType(final ObjectSpecification specification) { // do the comparison using value types because of a possible aliasing/race condition // in matchesParameterOf when building up contributed associations if (specification.getSpecId().equals(this.getSpecId())) { return true; } for (final ObjectSpecification interfaceSpec : interfaces()) { if (interfaceSpec.isOfType(specification)) { return true; } } final ObjectSpecification superclassSpec = superclass(); return superclassSpec != null ? superclassSpec.isOfType(specification) : false; }
@Override public ObjectAdapter loadInstanceAndAdapt(final TypedOid oid) throws ObjectNotFoundException, ObjectPersistenceException { if (LOG.isDebugEnabled()) { LOG.debug("getObject " + oid); } final ObjectSpecification objectSpec = getSpecificationLookup().lookupBySpecId(oid.getObjectSpecId()); final ObjectStoreInstances ins = instancesFor(objectSpec.getSpecId()); final ObjectAdapter adapter = ins.getObjectAndMapIfRequired(oid); if (adapter == null) { throw new ObjectNotFoundException(oid); } return adapter; }
public String getClassName() { final String fullIdentifier = spec.getFullIdentifier(); final int lastDot = fullIdentifier.lastIndexOf("."); return lastDot > 0 && lastDot < fullIdentifier.length() - 1 ? fullIdentifier.substring(lastDot + 1, fullIdentifier.length()) : fullIdentifier; }
@Override public ApplicationAdvice appliesTo(IModel<?> model) { if (!(model instanceof EntityCollectionModel)) { return ApplicationAdvice.DOES_NOT_APPLY; } EntityCollectionModel entityCollectionModel = (EntityCollectionModel) model; if (entityCollectionModel.hasSelectionHandler()) { return ApplicationAdvice.DOES_NOT_APPLY; } ObjectSpecification typeOfSpec = entityCollectionModel.getTypeOfSpecification(); ObjectSpecification pieChartableSpec = IsisContext.getSpecificationLoader().loadSpecification(PieChartable.class); return appliesIf(typeOfSpec.isOfType(pieChartableSpec)); }
@Programmatic @Override public Object homePage() { final List<ObjectAdapter> serviceAdapters = getPersistenceSession().getServices(); for (final ObjectAdapter serviceAdapter : serviceAdapters) { final ObjectSpecification serviceSpec = serviceAdapter.getSpecification(); final List<ObjectAction> objectActions = serviceSpec.getObjectActions(Contributed.EXCLUDED); for (final ObjectAction objectAction : objectActions) { final Object homePage = homePageIfUsable(serviceAdapter, objectAction); if (homePage != null) { return homePage; } } } return null; }
private void addIfReturnsSubtype( final ObjectAction serviceAction, final List<ObjectAction> matchingActionsToAppendTo) { final ObjectSpecification returnType = serviceAction.getReturnType(); if (returnType == null) { return; } if (returnType.isParentedOrFreeCollection()) { final TypeOfFacet facet = serviceAction.getFacet(TypeOfFacet.class); if (facet != null) { final ObjectSpecification elementType = facet.valueSpec(); addIfReturnsSubtype(serviceAction, elementType, matchingActionsToAppendTo); } } else { addIfReturnsSubtype(serviceAction, returnType, matchingActionsToAppendTo); } }
@Override public boolean hasInstances(final ObjectSpecification spec) { if (instancesFor(spec.getSpecId()).hasInstances()) { return true; } // includeSubclasses final List<ObjectSpecification> subclasses = spec.subclasses(); for (int i = 0; i < subclasses.size(); i++) { if (hasInstances(subclasses.get(i))) { return true; } } return false; }
private void addIfReturnsSubtype( final ObjectAction serviceAction, final ObjectSpecification actionType, final List<ObjectAction> matchingActionsToAppendTo) { if (actionType.isOfType(this)) { matchingActionsToAppendTo.add(serviceAction); } }
@Programmatic @SuppressWarnings("unchecked") @Override public <T> T newViewModelInstance(Class<T> ofClass, String memento) { final ObjectSpecification spec = specificationLoader.loadSpecification(ofClass); if (!spec.containsFacet(ViewModelFacet.class)) { throw new IsisException("Type must be a ViewModel: " + ofClass); } final ObjectAdapter adapter = persistenceSessionServiceInternal.createViewModelInstance(spec, memento); if (adapter.getOid().isViewModel()) { return (T) adapter.getObject(); } else { throw new IsisException( "Object instantiated but was not given a ViewModel Oid; please report as a possible defect in Isis: " + ofClass); } }
private ObjectAction findAction(final ObjectSpecification specification, final String actionId) { final List<ObjectAction> objectActions = specification.getObjectActions(Contributed.INCLUDED); for (final ObjectAction objectAction : objectActions) { if (objectAction.getIdentifier().toClassAndNameIdentityString().equals(actionId)) { return objectAction; } } return null; }
@Override public void validateSpecifications(ValidationFailures validationFailures) { final Map<ObjectSpecId, ObjectSpecification> specById = Maps.newHashMap(); for (final ObjectSpecification objSpec : allSpecifications()) { final ObjectSpecId objectSpecId = objSpec.getSpecId(); if (objectSpecId == null) { continue; } final ObjectSpecification existingSpec = specById.put(objectSpecId, objSpec); if (existingSpec == null) { continue; } validationFailures.add( "Cannot have two entities with same object type (@ObjectType facet or equivalent) Value; " + "both %s and %s are annotated with value of ''%s''.", existingSpec.getFullIdentifier(), objSpec.getFullIdentifier(), objectSpecId); } }
Property propertyFor(final ObjectSpecification objectSpecification) { final Property property = valuePropertyFactory.newProperty(objectSpecification.getCorrespondingClass()); if (property != null) { return property; } else { // assume this is a reference to an entity/view model, meaning we use an href return refToHrefModel(); } }
public boolean hasInstances(final ObjectSpecification specification) { ensureOpened(); ensureInTransaction(); if (LOG.isDebugEnabled()) { LOG.debug("hasInstances: class=" + specification.getFullIdentifier()); } if (!specification.persistability().isPersistable()) { LOG.warn("hasInstances: trying to run for non-persistent class " + specification); return false; } final Query query = QueryUtil.createQuery(getPersistenceManager(), "o", "select o.id", specification, null); throw new NotYetImplementedException(); // query.set.setMaxResults(1); // return !query.getResultList().isEmpty(); }
private static void specificationActionMethods( final ObjectSpecification specification, final DebugBuilder debugBuilder) { try { final List<ObjectAction> userActions = specification.getObjectActions( ActionType.USER, Contributed.INCLUDED, Filters.<ObjectAction>any()); final List<ObjectAction> explActions = specification.getObjectActions( ActionType.EXPLORATION, Contributed.INCLUDED, Filters.<ObjectAction>any()); final List<ObjectAction> prototypeActions = specification.getObjectActions( ActionType.PROTOTYPE, Contributed.INCLUDED, Filters.<ObjectAction>any()); final List<ObjectAction> debActions = specification.getObjectActions( ActionType.DEBUG, Contributed.INCLUDED, Filters.<ObjectAction>any()); specificationMethods(userActions, explActions, prototypeActions, debActions, debugBuilder); } catch (final RuntimeException e) { debugBuilder.appendException(e); } }