⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model.java

📁 数据仓库展示程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            this.name = name;
        }

        protected Model getModel() {
            return entity.model;
        }

        public String toString() {
            return entity + "." + name;
        }
    }

    static abstract class Attribute extends Member {
        Attribute(Entity entity, java.lang.reflect.Member member, String name) {
            super(entity, member, name);
        }

        public String getDescription() {
            return toString() + ": " + getShortName(getType());
        }

        abstract Class getType();

        public abstract Object getValue(Object o);
    }

    static class FieldAttribute extends Attribute {
        FieldAttribute(Entity entity, Field field, String name) {
            super(entity, field, name);
        }

        Class getType() {
            return getField().getType();
        }

        private Field getField() {
            return (Field) member;
        }

        public Object getValue(Object o) {
            try {
                return getField().get(o);
            } catch (IllegalArgumentException e) {
                throw Util.newInternal(e, "Error while printing attribute");
            } catch (IllegalAccessException e) {
                throw Util.newInternal(e, "Error while printing attribute");
            }
        }
    }

    static class MethodAttribute extends Attribute {
        MethodAttribute(Entity entity, Method method, String name) {
            super(entity, method, name);
        }

        Class getType() {
            return getMethod().getReturnType();
        }

        private Method getMethod() {
            return (Method) member;
        }

        public Object getValue(Object o) {
            try {
                return getMethod().invoke(o, null);
            } catch (IllegalAccessException e) {
                throw Util.newInternal(e, "Error while getting attribute value");
            } catch (IllegalArgumentException e) {
                throw Util.newInternal(e, "Error while getting attribute value");
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof UnsupportedOperationException) {
                    return null;
                }
                throw Util.newInternal(e, "Error while getting attribute value");
            }
        }
    }

    static class Relationship extends Member {
        private Relationship inverse;
        private Method getMethod;

        Relationship(Entity entity, java.lang.reflect.Member member, String name) {
            super(entity, member, name);
            if (member != null) {
                final String getMethodName = "get" + member.getName().substring("remove".length());
                try {
                    this.getMethod = entity.clazz.getMethod(getMethodName, emptyClassArray);
                } catch (NoSuchMethodException e) {
                    throw Util.newInternal(e, "Error while traversing relationship");
                } catch (SecurityException e) {
                    throw Util.newInternal(e, "Error while traversing relationship");
                }
            }
        }

        public String getDescription() {
            return toString() + ": " + getShortName(getTargetType()) +
                    (inverse == null ? "" : " (inverse " + inverse.name + ")");
        }

        Class getTargetType() {
            if (member instanceof Method) {
                final Method method = (Method) member;
                Util.assertTrue(method.getName().startsWith("remove") &&
                        method.getParameterTypes().length == 1);
                return method.getParameterTypes()[0];
            } else {
                throw Util.newInternal(member + " should be a Method");
            }
        }

        Relationship getInverse() {
            if (inverse == null) {
                inverse = computeInverse();
            }
            if (inverse == noRelationship) {
                return null;
            }
            return inverse;
        }

        private Relationship computeInverse() {
            Class targetClass = getTargetType();
            Entity targetEntity = getModel().getEntityForClass(targetClass);
            Relationship[] relationships = targetEntity.getDeclaredRelationships();
            Member candidate = null;
            int candidateCount = 0;
            for (int i = 0; i < relationships.length; i++) {
                Relationship relationship = relationships[i];
                if (relationship.getTargetType() == entity.clazz) {
                    candidate = relationship;
                    candidateCount++;
                }
            }
            Attribute[] attributes = targetEntity.getDeclaredAttributes();
            for (int i = 0; i < attributes.length; i++) {
                Attribute attribute = attributes[i];
                if (attribute.getType() == entity.clazz) {
                    candidate = attribute;
                    candidateCount++;
                }
            }
            if (candidateCount == 1) {
                return (Relationship) candidate;
            } else {
            	LOGGER.debug("Found " + candidateCount + " candidates for inverse of " + this);
                return null;
            }
        }

        boolean isContains() {
            return isMany() && entity.ordinal < getModel().getEntity(getTargetType()).ordinal;
        }

        private boolean isMany() {
            return member.getName().startsWith("remove");
        }

        public Collection getTargets(Object o) {
            try {
                Object result = getMethod.invoke(o, null);
                if (result instanceof Collection) {
                    return (Collection) result;
                } else {
                    throw Util.newInternal("Cannot convert " +
                            result.getClass() + " in to a collection");
                }
            } catch (IllegalAccessException e) {
                throw Util.newInternal(e, "Error while traversing relationship");
            } catch (IllegalArgumentException e) {
                throw Util.newInternal(e, "Error while traversing relationship");
            } catch (InvocationTargetException e) {
                throw Util.newInternal(e, "Error while traversing relationship");
            }
        }
    }

    public static void main(String[] args) {
        foo(Class.class);
    }

    public static void foo(Object o) {
        Entity entity = Model.instance().getEntity(o);
        final Attribute[] attributes = entity.getAttributes();
        for (int i = 0; i < attributes.length; i++) {
            Attribute attribute = attributes[i];
            LOGGER.debug("Attribute: " + attribute.getDescription());
                    }
        final Relationship[] relationships = entity.getRelationships();
        for (int i = 0; i < relationships.length; i++) {
            Relationship relationship = relationships[i];
            LOGGER.debug("Relationship: " + relationship.getDescription());
        }
    }

    /**
     * Serializes an object, its attributes, and contained objects as XML.
     */
    public void toXML(Object o, PrintWriter pw) {
        toXML(o, pw, new HashSet(), 0);
    }

    private void toXML(Object o, PrintWriter pw, HashSet active, int indent) {
        Entity entity = Model.instance().getEntity(o);
        for (int i = 0; i < indent; i++) {
            pw.print("\t");
        }
        pw.print("<" + entity);
        int relationshipCount = 0;
        final Attribute[] attributes = entity.getAttributes();
        for (int i = 0; i < attributes.length; i++) {
            Attribute attribute = attributes[i];
            final Object value = attribute.getValue(o);
            if (value != null) {
                pw.print(" " + attribute.name + "=\"" + value + "\"");
            }
        }
        final Relationship[] relationships = entity.getRelationships();
        for (int i = 0; i < relationships.length; i++) {
            if (relationshipCount++ == 0) {
                pw.println(">");
            }
            Relationship relationship = relationships[i];
            Collection targets = relationship.getTargets(o);
            for (Iterator iterator = targets.iterator(); iterator.hasNext();) {
                Object target = iterator.next();
                if (active.add(target)) {
                    toXML(target, pw, active, indent + 1);
                    active.remove(target);
                }
            }
        }
        if (relationshipCount == 0) {
            pw.println("/>");
        } else {
            for (int i = 0; i < indent; i++) {
                pw.print("\t");
            }
            pw.println("</" + entity + ">");
        }
    }
}

// End Model.java

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -