private static Map<String, BootstrapPackage> getPackagesMap(Database database) { Map<String, BootstrapPackage> packagesByName = new TreeMap<String, BootstrapPackage>(); // First create the _init package for the whole database BootstrapPackage initPkg = new BootstrapPackage(); initPkg.setName(INIT_NAME); initPkg.setIsInit(true); packagesByName.put(INIT_NAME, initPkg); for (ObjectType type : database.getEnvironment().getTypes()) { TypeData bmod = type.as(TypeData.class); for (String name : bmod.getPackageNames()) { BootstrapPackage pkg = packagesByName.get(name); if (pkg == null) { pkg = new BootstrapPackage(); pkg.setName(name); packagesByName.put(name, pkg); } pkg.getTypes().add(type); } } for (BootstrapPackage pkg : Query.from(BootstrapPackage.class).using(database).selectAll()) { packagesByName.put(pkg.getName(), pkg); } for (Map.Entry<String, BootstrapPackage> entry : packagesByName.entrySet()) { checkConsistency( database, entry.getValue(), new HashSet<BootstrapPackage>(packagesByName.values()), null); } return packagesByName; }
public static Set<ObjectType> getAllTypes(Database database, Set<ObjectType> types) { Set<ObjectType> allTypes = new HashSet<ObjectType>(); for (ObjectType type : types) { allTypes.addAll(database.getEnvironment().getTypesByGroup(type.getInternalName())); } return allTypes; }
/** * Resolves a type by looking up its first component in the scope, and subsequent components as * properties. The scope must have been fully parsed and a symbol table constructed. * * @return The type of the symbol, or null if the type could not be found. */ private JSType lookupViaProperties(ErrorReporter reporter, StaticTypedScope<JSType> enclosing) { String[] componentNames = reference.split("\\.", -1); if (componentNames[0].length() == 0) { return null; } StaticTypedSlot<JSType> slot = enclosing.getSlot(componentNames[0]); if (slot == null) { return null; } // If the first component has a type of 'Unknown', then any type // names using it should be regarded as silently 'Unknown' rather than be // noisy about it. JSType slotType = slot.getType(); if (slotType == null || slotType.isAllType() || slotType.isNoType()) { return null; } JSType value = getTypedefType(reporter, slot); if (value == null) { return null; } // resolving component by component for (int i = 1; i < componentNames.length; i++) { ObjectType parentClass = ObjectType.cast(value); if (parentClass == null) { return null; } if (componentNames[i].length() == 0) { return null; } value = parentClass.getPropertyType(componentNames[i]); } return value; }
@Override public void initialize(Cache cache) { int count = 0; try { Container container = Container.decode(cache.getStore().read(CacheIndex.REFERENCE, CacheIndex.CONFIGS)); ReferenceTable table = ReferenceTable.decode(container.getData()); Entry entry = table.getEntry(ConfigArchive.OBJECT); Archive archive = Archive.decode( cache.read(CacheIndex.CONFIGS, ConfigArchive.OBJECT).getData(), entry.size()); objs = new ObjectType[entry.capacity()]; for (int id = 0; id < entry.capacity(); id++) { ChildEntry child = entry.getEntry(id); if (child == null) continue; ByteBuffer buffer = archive.getEntry(child.index()); ObjectType type = new ObjectType(id); type.decode(buffer); objs[id] = type; count++; } } catch (IOException e) { logger.log(Level.SEVERE, "Error Loading ObjectType(s)!", e); } logger.info("Loaded " + count + " ObjectType(s)!"); }
@Override protected List<DynamicProperty> create() { List<DynamicProperty> properties = new ArrayList<DynamicProperty>(); for (ObjectType type : getTypes()) { if (type.getObjectClass() == null) { continue; } String beanProperty = type.getJavaBeanProperty(); if (ObjectUtils.isBlank(beanProperty)) { continue; } try { properties.add( new DynamicProperty(type, beanProperty, beanPropertyIndexes.get(beanProperty))); } catch (Exception error) { // Failed introspection so probably not a valid bean // property. } } return ImmutableList.copyOf(properties); }
@SuppressWarnings("deprecation") public static void attachment(String id, String field) throws Exception { ObjectType type = ObjectType.get(getControllerClass()); notFoundIfNull(type); Model object = type.findById(id); notFoundIfNull(object); Object att = object.getClass().getField(field).get(object); if (att instanceof Model.BinaryField) { Model.BinaryField attachment = (Model.BinaryField) att; if (attachment == null || !attachment.exists()) { notFound(); } response.contentType = attachment.type(); renderBinary(attachment.get(), attachment.length()); } // DEPRECATED if (att instanceof play.db.jpa.FileAttachment) { play.db.jpa.FileAttachment attachment = (play.db.jpa.FileAttachment) att; if (attachment == null || !attachment.exists()) { notFound(); } renderBinary(attachment.get(), attachment.filename); } notFound(); }
@Override public void format(HtmlWriter writer) throws IOException { writer.writeStart("pre"); State state = getState(); ObjectType type = state.getType(); if (type != null) { writer.writeHtml(type.getInternalName()); writer.writeHtml(": "); } writer.writeStart( "a", "target", "_blank", "href", StringUtils.addQueryParameters( "/_debug/query", "where", "id = " + state.getId(), "action", "Run")); writer.writeHtml(getLabel()); writer.writeEnd(); writer.writeElement("br"); writer.writeHtml(ObjectUtils.toJson(state.getSimpleValues(), true)); writer.writeEnd(); }
/** Adds the given {@code type} to all type cache maps. */ public void add(ObjectType type) { String className = type.getObjectClassName(); if (!ObjectUtils.isBlank(className)) { byClassName.put(className.toLowerCase(Locale.ENGLISH), type); } byId.put(type.getId(), type); String internalName = type.getInternalName(); if (!ObjectUtils.isBlank(internalName)) { byName.put(internalName.toLowerCase(Locale.ENGLISH), type); } for (String group : type.getGroups()) { if (group != null) { group = group.toLowerCase(Locale.ENGLISH); } Set<ObjectType> groupTypes = byGroup.get(group); if (groupTypes == null) { groupTypes = new HashSet<ObjectType>(); byGroup.put(group, groupTypes); } groupTypes.remove(type); groupTypes.add(type); } }
@Override protected void run() { // Fetch the globals, which includes a reference to the root // type. References to other objects can't be resolved, // because the type definitions haven't been loaded yet. refreshGlobals(); ObjectType rootType = getRootType(); if (rootType != null) { // This isn't cleared later, because that's done within // {@link refreshTypes} anyway. TypesCache temporaryTypes = temporaryTypesLocal.get(); if (temporaryTypes == null) { temporaryTypes = new TypesCache(); temporaryTypesLocal.set(temporaryTypes); } temporaryTypes.add(rootType); LOGGER.info("Root type ID for [{}] is [{}]", getDatabase().getName(), rootType.getId()); } // Load all other types based on the root type. Then globals // again in case they reference other typed objects. Then // types again using the information from the fully resolved // globals. refreshTypes(); refreshGlobals(); refreshTypes(); refresher.scheduleWithFixedDelay(5.0, 5.0); }
/** * Given a constructor or an interface type, get its superclass constructor or {@code null} if * none exists. */ public FunctionType getSuperClassConstructor() { Preconditions.checkArgument(isConstructor() || isInterface()); ObjectType maybeSuperInstanceType = getPrototype().getImplicitPrototype(); if (maybeSuperInstanceType == null) { return null; } return maybeSuperInstanceType.getConstructor(); }
@Override public Iterable<ObjectType> getCtorExtendedInterfaces() { LinkedHashSet<ObjectType> resolvedExtendedInterfaces = new LinkedHashSet<>(); for (ObjectType obj : getReferencedObjTypeInternal().getCtorExtendedInterfaces()) { resolvedExtendedInterfaces.add(obj.visit(replacer).toObjectType()); } return resolvedExtendedInterfaces; }
public static ObjectType fromInt(int enumValue) { for (ObjectType objectType : values()) { if (objectType.getValue() == enumValue) { return objectType; } } return null; }
private boolean implicitPrototypeChainIsUnknown() { ObjectType p = getImplicitPrototype(); while (p != null) { if (p.isUnknownType()) { return true; } p = p.getImplicitPrototype(); } return false; }
@Override void collectPropertyNames(Set<String> props) { for (String prop : properties.keySet()) { props.add(prop); } ObjectType implicitPrototype = getImplicitPrototype(); if (implicitPrototype != null) { implicitPrototype.collectPropertyNames(props); } }
JSType getGreatestSubtypeHelper(JSType that) { if (that.isRecordType()) { RecordType thatRecord = that.toMaybeRecordType(); RecordTypeBuilder builder = new RecordTypeBuilder(registry); builder.setSynthesized(true); // The greatest subtype consists of those *unique* properties of both // record types. If any property conflicts, then the NO_TYPE type // is returned. for (String property : getOwnPropertyNames()) { if (thatRecord.hasProperty(property) && !thatRecord.getPropertyType(property).isInvariant(getPropertyType(property))) { return registry.getNativeObjectType(JSTypeNative.NO_TYPE); } builder.addProperty(property, getPropertyType(property), getPropertyNode(property)); } for (String property : thatRecord.getOwnPropertyNames()) { if (!hasProperty(property)) { builder.addProperty( property, thatRecord.getPropertyType(property), thatRecord.getPropertyNode(property)); } } return builder.build(); } JSType greatestSubtype = registry.getNativeType(JSTypeNative.NO_OBJECT_TYPE); JSType thatRestrictedToObj = registry.getNativeType(JSTypeNative.OBJECT_TYPE).getGreatestSubtype(that); if (!thatRestrictedToObj.isEmptyType()) { // In this branch, the other type is some object type. We find // the greatest subtype with the following algorithm: // 1) For each property "x" of this record type, take the union // of all classes with a property "x" with a compatible property type. // and which are a subtype of {@code that}. // 2) Take the intersection of all of these unions. for (String propName : getOwnPropertyNames()) { JSType propType = getPropertyType(propName); UnionTypeBuilder builder = new UnionTypeBuilder(registry); for (ObjectType alt : registry.getEachReferenceTypeWithProperty(propName)) { JSType altPropType = alt.getPropertyType(propName); if (altPropType != null && !alt.isEquivalentTo(this) && alt.isSubtype(that) && propType.isInvariant(altPropType)) { builder.addAlternate(alt); } } greatestSubtype = greatestSubtype.getLeastSupertype(builder.build()); } } return greatestSubtype; }
public static void show(String id) throws Exception { ObjectType type = ObjectType.get(getControllerClass()); notFoundIfNull(type); Model object = type.findById(id); notFoundIfNull(object); try { render(type, object); } catch (TemplateNotFoundException e) { render("CRUD/show.html", type, object); } }
public void setExtendedInterfaces(List<ObjectType> extendedInterfaces) throws UnsupportedOperationException { if (isInterface()) { this.extendedInterfaces = ImmutableList.copyOf(extendedInterfaces); for (ObjectType extendedInterface : this.extendedInterfaces) { typeOfThis.extendTemplateTypeMap(extendedInterface.getTemplateTypeMap()); } } else { throw new UnsupportedOperationException(); } }
@Override protected void beforeSave() { super.beforeSave(); if (StringUtils.isBlank(getName())) { // To avoid {@link com.psddev.dari.db.ValidationException} error as it implements {@link // Singleton}. ObjectType objectType = ObjectType.getInstance(getClass()); setName(objectType.getDisplayName()); } }
/** * Resolve each item in the list, and return a new list if any references changed. Otherwise, * return null. */ private ImmutableList<ObjectType> resolveTypeListHelper( ImmutableList<ObjectType> list, ErrorReporter t, StaticScope<JSType> scope) { boolean changed = false; ImmutableList.Builder<ObjectType> resolvedList = ImmutableList.builder(); for (ObjectType type : list) { ObjectType resolved = (ObjectType) type.resolve(t, scope); resolvedList.add(resolved); changed |= (resolved != type); } return changed ? resolvedList.build() : null; }
private void finishPropertyContinuations() { ObjectType referencedObjType = getReferencedObjTypeInternal(); if (referencedObjType != null && !referencedObjType.isUnknownType() && propertyContinuations != null) { for (PropertyContinuation c : propertyContinuations) { c.commit(this); } } propertyContinuations = null; }
public void setImplementedInterfaces(List<ObjectType> implementedInterfaces) { if (isConstructor()) { // Records this type for each implemented interface. for (ObjectType type : implementedInterfaces) { registry.registerTypeImplementingInterface(this, type); typeOfThis.extendTemplateTypeMap(type.getTemplateTypeMap()); } this.implementedInterfaces = ImmutableList.copyOf(implementedInterfaces); } else { throw new UnsupportedOperationException(); } }
/** Creates an object represented by the given {@code typeId} and {@code id}. */ public Object createObject(UUID typeId, UUID id) { bootstrapOnce.ensure(); Class<?> objectClass = null; ObjectType type = null; if (typeId != null && !GLOBALS_ID.equals(id)) { if (typeId.equals(id)) { objectClass = ObjectType.class; } else { type = getTypeById(typeId); if (type != null) { objectClass = type.isAbstract() ? Record.class : type.getObjectClass(); } } } boolean hasClass = true; if (objectClass == null) { objectClass = Record.class; hasClass = false; } Object object = SubstitutionUtils.newInstance(objectClass); State state; try { state = State.getInstance(object); } catch (IllegalArgumentException error) { object = TypeDefinition.getInstance(Record.class).newInstance(); state = State.getInstance(object); hasClass = false; } state.setDatabase(getDatabase()); state.setId(id); state.setTypeId(typeId); if (type != null) { if (!hasClass) { for (ObjectField field : type.getFields()) { Object defaultValue = field.getDefaultValue(); if (defaultValue != null) { state.put(field.getInternalName(), defaultValue); } } } } return object; }
@Override public boolean isPropertyInExterns(String propertyName) { Property p = properties.get(propertyName); if (p != null) { return p.isFromExterns(); } ObjectType implicitPrototype = getImplicitPrototype(); if (implicitPrototype != null) { return implicitPrototype.isPropertyInExterns(propertyName); } return false; }
@Override public Node getPropertyNode(String propertyName) { Property p = properties.get(propertyName); if (p != null) { return p.getNode(); } ObjectType implicitPrototype = getImplicitPrototype(); if (implicitPrototype != null) { return implicitPrototype.getPropertyNode(propertyName); } return null; }
/** * Given an interface and a property, finds the top-most super interface that has the property * defined (including this interface). */ public static ObjectType getTopDefiningInterface(ObjectType type, String propertyName) { ObjectType foundType = null; if (type.hasProperty(propertyName)) { foundType = type; } for (ObjectType interfaceType : type.getCtorExtendedInterfaces()) { if (interfaceType.hasProperty(propertyName)) { foundType = getTopDefiningInterface(interfaceType, propertyName); } } return foundType; }
public void matchRecordTypeConstraint(ObjectType constraintObj) { for (String prop : constraintObj.getOwnPropertyNames()) { JSType propType = constraintObj.getPropertyType(prop); if (!isPropertyTypeDeclared(prop)) { JSType typeToInfer = propType; if (!hasProperty(prop)) { typeToInfer = getNativeType(JSTypeNative.VOID_TYPE).getLeastSupertype(propType); } defineInferredProperty(prop, typeToInfer, null); } } }
/** * Given the name of a native object property, checks whether the property is present on the * object and different from the native one. */ private boolean hasOverridenNativeProperty(String propertyName) { if (isNativeObjectType()) { return false; } JSType propertyType = getPropertyType(propertyName); ObjectType nativeType = this.isFunctionType() ? registry.getNativeObjectType(JSTypeNative.FUNCTION_PROTOTYPE) : registry.getNativeObjectType(JSTypeNative.OBJECT_PROTOTYPE); JSType nativePropertyType = nativeType.getPropertyType(propertyName); return propertyType != nativePropertyType; }
@Override JSType resolveInternal(ErrorReporter t, StaticScope<JSType> scope) { setResolvedTypeInternal(this); ObjectType implicitPrototype = getImplicitPrototype(); if (implicitPrototype != null) { implicitPrototypeFallback = (ObjectType) implicitPrototype.resolve(t, scope); } for (Property prop : properties.values()) { prop.setType(safeResolve(prop.getType(), t, scope)); } return this; }
public static void delete(String id) throws Exception { ObjectType type = ObjectType.get(getControllerClass()); notFoundIfNull(type); Model object = type.findById(id); notFoundIfNull(object); try { object._delete(); } catch (Exception e) { flash.error(play.i18n.Messages.get("crud.delete.error", type.modelName)); redirect(request.controller + ".show", object._key()); } flash.success(play.i18n.Messages.get("crud.deleted", type.modelName)); redirect(request.controller + ".list"); }
public boolean appliesTo(DBPObject object, Object context) { object = DBUtils.getPublicObject(object); if (object == null) { return false; } Object adapted = adaptType(object); for (ObjectType objectType : objectTypes) { if (objectType.appliesTo(object, context) || (adapted != null && objectType.appliesTo(adapted, context))) { return true; } } return false; }