Esempio n. 1
0
        public virtual string GetExtends(ClassMapping cmap)
        {
            string extendz = string.Empty;

            if (cmap.Interface)
            {
                if (cmap.SuperClassMapping != null && cmap.SuperClassMapping.Interface)
                {
                    extendz += cmap.SuperClassMapping.Name;
                }
                if (cmap.GetMeta(extendz) != null)
                {
                    if ((Object)extendz != null)
                    {
                        extendz += ",";
                    }
                    extendz = cmap.GetMetaAsString("extends");
                }
            }
            else if ((Object)cmap.SuperClass != null)
            {
                if (cmap.SuperClassMapping != null && cmap.SuperClassMapping.Interface)
                {
                    // class cannot extend it's superclass because the superclass is marked as an interface
                }
                else
                {
                    extendz = cmap.SuperClass;
                }
            }
            else if (cmap.GetMeta("extends") != null)
            {
                extendz = cmap.GetMetaAsString("extends");
            }

            return(extendz);
        }
Esempio n. 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();
        }
Esempio n. 3
0
        public override void Render(string savedToPackage, string savedToClass, ClassMapping classMapping,
                                    IDictionary class2classmap, StreamWriter mainwriter)
        {
            mainwriter.WriteLine("using System;");
            mainwriter.WriteLine(
                @"//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------
");
            GeneratePackageDelaration(savedToPackage, classMapping, mainwriter);
            mainwriter.WriteLine("{");

            // switch to another writer to be able to insert the actually
            // used imports when whole class has been rendered.
            StringWriter writer = new StringWriter();


            // class declaration
            if (classMapping.GetMeta("class-description") == null)
            {
                writer.WriteLine(
                    @" /// <summary>
 /// POJO for {0}
 /// </summary>
 /// <remark>
 /// This class is autogenerated
 /// </remark>
",
                    classMapping.ClassName);
            }
            else
            {
                writer.WriteLine("/// <summary>\n" + languageTool.ToJavaDoc(classMapping.GetMetaAsString("class-description"), 0) +
                                 "\n/// </summary>");
            }

            string classScope      = classMapping.Scope;
            string declarationType = classMapping.DeclarationType;


            //classMapping.addImport(typeof(System.Runtime.Serialization.ISerializable));
            //string modifiers = classMapping.getModifiers();
            if (classMapping.ShouldBeAbstract() && (classScope.IndexOf("abstract") == -1))
            {
                writer.Write("abstract " + classScope + " " + declarationType + " " + savedToClass);
            }
            else
            {
                writer.Write(classScope + " " + declarationType + " " + savedToClass);
            }
            if (languageTool.HasExtends(classMapping) || languageTool.HasImplements(classMapping))
            {
                writer.Write(" : ");
            }

            if (languageTool.HasExtends(classMapping))
            {
                writer.Write(languageTool.GetExtends(classMapping));
            }

            if (languageTool.HasExtends(classMapping) && languageTool.HasImplements(classMapping))
            {
                writer.Write(", ");
            }

            if (languageTool.HasImplements(classMapping))
            {
                writer.Write(languageTool.GetImplements(classMapping));
            }

            writer.WriteLine(" {");
            writer.WriteLine();

            // switch to another writer to be able to insert the
            // veto- and changeSupport fields
            StringWriter propWriter = new StringWriter();

            if (!classMapping.Interface)
            {
                DoFields(classMapping, class2classmap, propWriter);
                DoConstructors(savedToClass, classMapping, class2classmap, propWriter);
            }

            string vetoSupport   = MakeSupportField("vetos", classMapping.AllFields);
            string changeSupport = MakeSupportField("changes", classMapping.AllFields);
            int    fieldTypes    = DoFieldAccessors(classMapping, class2classmap, propWriter, vetoSupport, changeSupport);

            if (!classMapping.Interface)
            {
                DoSupportMethods(fieldTypes, vetoSupport, changeSupport, propWriter);

                DoToString(classMapping, propWriter);

                DoEqualsAndHashCode(savedToClass, classMapping, propWriter);
            }
            if (classMapping.GetMeta("class-code") != null)
            {
                propWriter.WriteLine("// The following is extra code specified in the hbm.xml files");
                SupportClass.ListCollectionSupport extras = classMapping.GetMeta("class-code");
                IEnumerator iter = extras.GetEnumerator();
                while (iter.MoveNext())
                {
                    string code = iter.Current.ToString();
                    propWriter.WriteLine(code);
                }

                propWriter.WriteLine("// end of extra code specified in the hbm.xml files");
            }

            propWriter.WriteLine("}");

            //insert change and VetoSupport
            if (!classMapping.Interface)
            {
                DoSupports(fieldTypes, classMapping, vetoSupport, changeSupport, writer);
            }

            writer.Write(propWriter.ToString());

            // finally write the imports
            DoImports(classMapping, mainwriter);
            mainwriter.Write(writer.ToString());
            mainwriter.WriteLine("\n}");
        }