/* 
   EOModel+GSDoc.m

   Copyright (C) 2000 Free Software Foundation, Inc.

   Author: Manuel Guesdon <mguesdon@orange-concept.com>
   Date: August 2000

   This file is part of the GNUstep Database Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <eoaccess/common.h>
#include <eoaccess/EOModel.h>
#include <eoaccess/EOEntity.h>
#include <Foundation/Foundation.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSDebug.h>
#include "NSArray+GSDoc.h"
#include "NSDictionary+GSDoc.h"
#include "EOEntity+GSDoc.h"
#include "EOModel+GSDoc.h"


@implementation EOModel (GSDoc)
-(NSString*)gsdocContent
{
  return [self gsdocContentSplittedByEntities:NULL];
};

-(NSString*)gsdocContentSplittedByEntities:(NSDictionary**)entitiesPtr
{
  NSAutoreleasePool* arp=[NSAutoreleasePool new];
  NSString* content=[NSString string];
  int count=[entities count];
  int i=0;
  NSLog(@"Start: %@",[self class]);
  content=[content stringByAppendingFormat:@"<chapter%@>\n<heading>EOModel %@</heading>\n<EOModel%@%@%@%@>\n",
				   ([self name] ? [NSString stringWithFormat:@" id=\"%@\"",[self name]] : @""),
				   ([self name] ? [self name] : @""),
				   ([self name] ? [NSString stringWithFormat:@" name=\"%@\"",[self name]] : @""),
				   (version ? [NSString stringWithFormat:@" version=\"%@\"",version] : @""),
				   (adaptorName ? [NSString stringWithFormat:@" adaptorName=\"%@\"",adaptorName] : @""),
				   (adaptorClassName ? [NSString stringWithFormat:@" adaptorClassName=\"%@\"",adaptorClassName] : @"")];
  if (connectionDictionary)
	content=[content stringByAppendingString:[connectionDictionary gsdocContentWithTagName:@"EOConnectionDictionary"]];

  if (entitiesPtr)
	{
	  *entitiesPtr=[NSMutableDictionary dictionary];
	  content=[content stringByAppendingString:@"[[entities]]"];
	};

  for(i=0;i<count;i++)
	{
	  EOEntity* entity=[entities objectAtIndex:i];	  
	  NSString* entityContent=[entity gsdocContent];
	  NSAssert(entityContent,@"No entity gsdoc content");
	  if (entitiesPtr)
		{
		  entityContent=[NSString stringWithFormat:@"<chapter id=\"%@\">\n<heading>EOEntity %@</heading>\n%@\n</chapter>\n",
								  [entity name],
								  [entity name],
								  entityContent];
		  [(NSMutableDictionary*)*entitiesPtr setObject:entityContent
								 forKey:[entity name]];
		}
	  else
		content=[content stringByAppendingString:entityContent];
	};

  if ([userDictionary count])
	content=[content stringByAppendingString:[userDictionary gsdocContentWithTagName:@"EOUserDictionary"]];

  if ([self docComment])
	content=[content stringByAppendingFormat:@"<desc>%@</desc>\n",[self docComment]];
  
  content=[content stringByAppendingString:@"</EOModel>\n</chapter>\n"];
  NSLog(@"Stop: %@",[self class]);
  RETAIN(content);
  RETAIN(*entitiesPtr);
  DESTROY(arp);
  AUTORELEASE(*entitiesPtr);
  return AUTORELEASE(content);
};

@end


