public boolean run()

in openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ApplicationIdTool.java [277:447]


    public boolean run() {
        if (_log.isInfoEnabled())
            _log.info(_loc.get("appid-start", _type));

        // ensure that this type is a candidate for application identity
        if (_meta == null
            || _meta.getIdentityType() != ClassMetaData.ID_APPLICATION
            || _meta.isOpenJPAIdentity()) {
            if (!_ignore)
                throw new UserException(_loc.get("appid-invalid", _type));

            // else just warn
            if (_log.isWarnEnabled())
                _log.warn(_loc.get("appid-warn", _type));
            return false;
        }

        Class oidClass = _meta.getObjectIdType();
        Class superOidClass = null;

        // allow diff oid class in subclass (horizontal)
        if (_meta.getPCSuperclass() != null) {
            superOidClass = _meta.getPCSuperclassMetaData().getObjectIdType();
            if (oidClass == null || oidClass.equals(superOidClass)) {
                // just warn
                if (_log.isWarnEnabled())
                    _log.warn(_loc.get("appid-warn", _type));
                return false;
            }
        }

        // ensure that an id class is declared
        if (oidClass == null)
            throw new UserException(_loc.get("no-id-class", _type)).
                setFatal(true);

        // ensure there is at least one pk field if we are
        // non-absract, and see if we have any byte[]
        boolean bytes = false;
        for (int i = 0; !bytes && i < _fields.length; i++)
            bytes = _fields[i].getDeclaredType() == byte[].class;

        // collect info on id type
        String className = getClassName();
        String packageName = ClassUtil.getPackageName(oidClass);
        String packageDec = "";
        if (packageName.length() > 0)
            packageDec = "package " + packageName + ";";

        String imports = getImports();
        String fieldDecs = getFieldDeclarations();
        String constructor = getConstructor(superOidClass != null);
        String properties = getProperties();
        String fromStringCode = getFromStringCode(superOidClass != null);
        String toStringCode = getToStringCode(superOidClass != null);
        String equalsCode = getEqualsCode(superOidClass != null);
        String hashCodeCode = getHashCodeCode(superOidClass != null);

        // build the java code
        CodeFormat code = newCodeFormat();
        if (!isInnerClass() && packageDec.length() > 0)
            code.append(packageDec).afterSection();

        if (!isInnerClass() && imports.length() > 0)
            code.append(imports).afterSection();

        code.append("/**").endl().
            append(" * ").
            append(_loc.get("appid-comment-for", _type.getName())).
            endl().
            append(" *").endl().
            append(" * ").append(_loc.get("appid-comment-gen")).endl().
            append(" * ").append(getClass().getName()).endl().
            append(" */").endl();
        code.append("public ");
        if (isInnerClass())
            code.append("static ");
        code.append("class ").append(className);
        if (code.getBraceOnSameLine())
            code.append(" ");
        else
            code.endl().tab();

        if (superOidClass != null) {
            code.append("extends " + ClassUtil.getClassName(superOidClass));
            if (code.getBraceOnSameLine())
                code.append(" ");
            else
                code.endl().tab();
        }
        code.append("implements Serializable").openBrace(1).endl();

        // if we use a byte array we need a static array for encoding to string
        if (bytes) {
            code.tab().append("private static final char[] HEX = ").
                append("new char[] {").endl();
            code.tab(2).append("'0', '1', '2', '3', '4', '5', '6', '7',").
                endl();
            code.tab(2).append("'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'").
                endl();
            code.tab().append("};").endl(2);
        }

        // static block to register class
        code.tab().append("static").openBrace(2).endl();
        code.tab(2).append("// register persistent class in JVM").endl();
        code.tab(2).append("try { Class.forName").openParen(true).
                append("\"").append(_type.getName()).append("\"").
                closeParen().append(";").append(" }").endl();
        code.tab(2).append("catch").openParen(true).
                append("Exception e").closeParen().append(" {}").endl();

        code.closeBrace(2);

        // field declarations
        if (fieldDecs.length() > 0)
            code.endl(2).append(fieldDecs);

        // default constructor
        code.afterSection().tab().append("public ").append(className).
            parens().openBrace(2).endl();
        code.closeBrace(2);

        // string constructor
        code.afterSection().append(constructor);

        // properties
        if (properties.length() > 0)
            code.afterSection().append(properties);

        // toString, equals, hashCode methods
        if (toStringCode.length() > 0)
            code.afterSection().append(toStringCode);
        if (hashCodeCode.length() > 0)
            code.afterSection().append(hashCodeCode);
        if (equalsCode.length() > 0)
            code.afterSection().append(equalsCode);
        if (fromStringCode.length() > 0)
            code.afterSection().append(fromStringCode);

        // if we have any byte array fields, we have to add the extra
        // methods for handling byte arrays
        if (bytes) {
            code.afterSection().append(getToBytesByteArrayCode());
            code.afterSection().append(getToStringByteArrayCode());
            code.afterSection().append(getEqualsByteArrayCode());
            code.afterSection().append(getHashCodeByteArrayCode());
        }

        // base classes might need to define a custom tokenizer
        if (superOidClass == null && getTokenizer(false) == TOKENIZER_CUSTOM)
            code.afterSection().append(getCustomTokenizerClass());

        code.endl();
        code.closeBrace(1);

        _code = code.toString();

        // if this is an inner class, then indent the entire
        // code unit one tab level
        if (isInnerClass()) {
            // indent the entire code block one level to make it
            // a propertly indented innder class
            _code = code.getTab() +
                    StringUtil.replace(_code,
                                       J2DoPrivHelper.getLineSeparator(),
                                       J2DoPrivHelper.getLineSeparator() + code.getTab());
        }

        return true;
    }