/// <summary>  Create finders for properties that have the <meta atttribute="finder-method">
        /// finderName</meta> block defined. Also, create a findAll(Session) method.
        /// 
        /// </summary>
        public virtual void DoFinders(ClassMapping classMapping, IDictionary class2classmap, StringWriter writer)
        {
            // Find out of there is a system wide way to get sessions defined
            string sessionMethod = classMapping.GetMetaAsString("session-method").Trim();

            // fields
            foreach (FieldProperty field in classMapping.Fields)
            {
                if (field.GetMeta(MT_FINDERMETHOD) != null)
                {
                    string finderName = field.GetMetaAsString(MT_FINDERMETHOD);

                    if ("".Equals(sessionMethod))
                    {
                        // Make the method signature require a session to be passed in
                        writer.WriteLine("    public static List " + finderName + "(Session session, " +
                                         LanguageTool.GetTrueTypeName(field, class2classmap) + " " + field.FieldName + ") " +
                                         "throws SQLException, HibernateException {");
                    }
                    else
                    {
                        // Use the session method to get the session to execute the query
                        writer.WriteLine("    public static List " + finderName + "(" +
                                         LanguageTool.GetTrueTypeName(field, class2classmap) + " " + field.FieldName + ") " +
                                         "throws SQLException, HibernateException {");
                        writer.WriteLine("        Session session = " + sessionMethod);
                    }

                    writer.WriteLine("        List finds = session.find(\"from " + classMapping.FullyQualifiedName + " as " +
                                     classMapping.Name.ToLower() + " where " + classMapping.Name.ToLower() + "." + field.FieldName +
                                     "=?\", " + GetFieldAsObject(false, field) + ", " + GetFieldAsHibernateType(false, field) + ");");
                    writer.WriteLine("        return finds;");
                    writer.WriteLine("    }");
                    writer.WriteLine();
                }
                else if (field.GetMeta(MT_FOREIGNFINDERMETHOD) != null)
                {
                    string finderName = field.GetMetaAsString(MT_FOREIGNFINDERMETHOD);
                    string fieldName = field.GetMetaAsString(MT_FOREIGNFINDERFIELD);
                    string joinFieldName = field.GetMetaAsString(MT_FOREIGNJOINFIELD);

                    // Build the query
                    QueryBuilder qb = new QueryBuilder();
                    qb.LocalClass = classMapping;
                    qb.SetForeignClass(field.ForeignClass, class2classmap, joinFieldName);

                    ClassMapping foreignClass = (ClassMapping) class2classmap[field.ForeignClass.FullyQualifiedName];
                    if (foreignClass == null)
                    {
                        // Can't find the class, return
                        log.Error("Could not find the class " + field.ForeignClass.Name);
                        return;
                    }
                    FieldProperty foreignField = null;

                    foreach (FieldProperty f in foreignClass.Fields)
                    {
                        if (f.FieldName.Equals(fieldName))
                        {
                            foreignField = f;
                        }
                    }
                    if (foreignField != null)
                    {
                        qb.AddCritera(foreignClass, foreignField, "=");
                    }
                    else
                    {
                        // Can't find the field, return
                        log.Error("Could not find the field " + fieldName + " that was supposed to be in class " + field.ForeignClass.Name);
                        return;
                    }

                    MethodSignatureBuilder msb = new MethodSignatureBuilder(finderName, "List", "public static");
                    if ("".Equals(sessionMethod))
                    {
                        // Make the method signature require a session to be passed in
                        msb.AddParameter("Session session");
                        /*
                        writer.println("    public static List " + finderName +
                        "(Session session, " + getTrueTypeName(foreignField, class2classmap) + " " + foreignField.getName() + ") "
                        + "throws SQLException, HibernateException {");*/
                    }
                    else
                    {
                        // Use the session method to get the session to execute the query
                        /*
                        writer.println("    public static List " + finderName +
                        "(" + getTrueTypeName(foreignField, class2classmap) + " " + foreignField.getName() + ") "
                        + "throws SQLException, HibernateException {");
                        writer.println("        Session session = " + sessionMethod);*/
                    }
                    // Always need the object we're basing the query on
                    msb.AddParameter(classMapping.Name + " " + classMapping.Name.ToLower());

                    // And the foreign class field
                    msb.AddParameter(LanguageTool.GetTrueTypeName(foreignField, class2classmap) + " " + foreignField.FieldName);

                    msb.AddThrows("SQLException");
                    msb.AddThrows("HibernateException");

                    writer.WriteLine("    " + msb.BuildMethodSignature());
                    if (!"".Equals(sessionMethod))
                    {
                        writer.WriteLine("        Session session = " + sessionMethod);
                    }

                    writer.WriteLine("        List finds = session.find(\"" + qb.Query + "\", " + qb.ParamsAsString + ", " +
                                     qb.ParamTypesAsString + ");");
                    writer.WriteLine("        return finds;");
                    writer.WriteLine("    }");
                    writer.WriteLine();
                }
            }

            // Create the findAll() method
            if ("".Equals(sessionMethod))
            {
                writer.WriteLine("    public static List findAll" + "(Session session) " +
                                 "throws SQLException, HibernateException {");
            }
            else
            {
                writer.WriteLine("    public static List findAll() " + "throws SQLException, HibernateException {");
                writer.WriteLine("        Session session = " + sessionMethod);
            }
            writer.WriteLine("        List finds = session.find(\"from " + classMapping.Name + " in class " +
                             classMapping.PackageName + "." + classMapping.Name + "\");");
            writer.WriteLine("        return finds;");
            writer.WriteLine("    }");
            writer.WriteLine();
        }
Beispiel #2
0
        /// <summary>  Create finders for properties that have the <meta atttribute="finder-method">
        /// finderName</meta> block defined. Also, create a findAll(Session) method.
        ///
        /// </summary>
        public virtual void DoFinders(ClassMapping classMapping, IDictionary class2classmap, StringWriter writer)
        {
            // Find out of there is a system wide way to get sessions defined
            string sessionMethod = classMapping.GetMetaAsString("session-method").Trim();

            // fields
            foreach (FieldProperty field in classMapping.Fields)
            {
                if (field.GetMeta(MT_FINDERMETHOD) != null)
                {
                    string finderName = field.GetMetaAsString(MT_FINDERMETHOD);

                    if ("".Equals(sessionMethod))
                    {
                        // Make the method signature require a session to be passed in
                        writer.WriteLine("    public static List " + finderName + "(Session session, " +
                                         LanguageTool.GetTrueTypeName(field, class2classmap) + " " + field.FieldName + ") " +
                                         "throws SQLException, HibernateException {");
                    }
                    else
                    {
                        // Use the session method to get the session to execute the query
                        writer.WriteLine("    public static List " + finderName + "(" +
                                         LanguageTool.GetTrueTypeName(field, class2classmap) + " " + field.FieldName + ") " +
                                         "throws SQLException, HibernateException {");
                        writer.WriteLine("        Session session = " + sessionMethod);
                    }

                    writer.WriteLine("        List finds = session.find(\"from " + classMapping.FullyQualifiedName + " as " +
                                     classMapping.Name.ToLower() + " where " + classMapping.Name.ToLower() + "." + field.FieldName +
                                     "=?\", " + GetFieldAsObject(false, field) + ", " + GetFieldAsHibernateType(false, field) + ");");
                    writer.WriteLine("        return finds;");
                    writer.WriteLine("    }");
                    writer.WriteLine();
                }
                else if (field.GetMeta(MT_FOREIGNFINDERMETHOD) != null)
                {
                    string finderName    = field.GetMetaAsString(MT_FOREIGNFINDERMETHOD);
                    string fieldName     = field.GetMetaAsString(MT_FOREIGNFINDERFIELD);
                    string joinFieldName = field.GetMetaAsString(MT_FOREIGNJOINFIELD);

                    // Build the query
                    QueryBuilder qb = new QueryBuilder();
                    qb.LocalClass = classMapping;
                    qb.SetForeignClass(field.ForeignClass, class2classmap, joinFieldName);

                    ClassMapping foreignClass = (ClassMapping)class2classmap[field.ForeignClass.FullyQualifiedName];
                    if (foreignClass == null)
                    {
                        // Can't find the class, return
                        log.Error("Could not find the class " + field.ForeignClass.Name);
                        return;
                    }
                    FieldProperty foreignField = null;

                    foreach (FieldProperty f in foreignClass.Fields)
                    {
                        if (f.FieldName.Equals(fieldName))
                        {
                            foreignField = f;
                        }
                    }
                    if (foreignField != null)
                    {
                        qb.AddCritera(foreignClass, foreignField, "=");
                    }
                    else
                    {
                        // Can't find the field, return
                        log.Error("Could not find the field " + fieldName + " that was supposed to be in class " + field.ForeignClass.Name);
                        return;
                    }

                    MethodSignatureBuilder msb = new MethodSignatureBuilder(finderName, "List", "public static");
                    if ("".Equals(sessionMethod))
                    {
                        // Make the method signature require a session to be passed in
                        msb.AddParameter("Session session");

                        /*
                         * writer.println("    public static List " + finderName +
                         * "(Session session, " + getTrueTypeName(foreignField, class2classmap) + " " + foreignField.getName() + ") "
                         + "throws SQLException, HibernateException {");*/
                    }
                    else
                    {
                        // Use the session method to get the session to execute the query

                        /*
                         * writer.println("    public static List " + finderName +
                         * "(" + getTrueTypeName(foreignField, class2classmap) + " " + foreignField.getName() + ") "
                         + "throws SQLException, HibernateException {");
                         + writer.println("        Session session = " + sessionMethod);*/
                    }
                    // Always need the object we're basing the query on
                    msb.AddParameter(classMapping.Name + " " + classMapping.Name.ToLower());

                    // And the foreign class field
                    msb.AddParameter(LanguageTool.GetTrueTypeName(foreignField, class2classmap) + " " + foreignField.FieldName);

                    msb.AddThrows("SQLException");
                    msb.AddThrows("HibernateException");

                    writer.WriteLine("    " + msb.BuildMethodSignature());
                    if (!"".Equals(sessionMethod))
                    {
                        writer.WriteLine("        Session session = " + sessionMethod);
                    }

                    writer.WriteLine("        List finds = session.find(\"" + qb.Query + "\", " + qb.ParamsAsString + ", " +
                                     qb.ParamTypesAsString + ");");
                    writer.WriteLine("        return finds;");
                    writer.WriteLine("    }");
                    writer.WriteLine();
                }
            }

            // Create the findAll() method
            if ("".Equals(sessionMethod))
            {
                writer.WriteLine("    public static List findAll" + "(Session session) " +
                                 "throws SQLException, HibernateException {");
            }
            else
            {
                writer.WriteLine("    public static List findAll() " + "throws SQLException, HibernateException {");
                writer.WriteLine("        Session session = " + sessionMethod);
            }
            writer.WriteLine("        List finds = session.find(\"from " + classMapping.Name + " in class " +
                             classMapping.PackageName + "." + classMapping.Name + "\");");
            writer.WriteLine("        return finds;");
            writer.WriteLine("    }");
            writer.WriteLine();
        }