Получение списка доступных действий в SQL

Материал из BiTel WiKi

Версия от 13:58, 6 марта 2012; Aardvark (Обсуждение | вклад)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Скрипт берёт все возможные действия из .xml из папки action и кладёт их в таблицу bgs_module_action_descr. Далее данную таблицу можно совместить с любой из разрешающих действия пользователям или группам пользователей таблицей (bgs_user_action, bgs_group_action) и получить разрешенные действия в текстовом виде.

Скрипт для 5.1 Автор: Амир Абзалилов

import bitel.billing.server.util.*;
 
import java.sql.*;
import org.w3c.dom.*;
import ru.bitel.common.*;
 
public void main( setup, con, conSlave )
{
	File actionsDir = new File( "actions" );
	File[] files = actionsDir.listFiles( new FileFilter(){ public boolean accept(File pathname) { 
		return pathname.isFile() && pathname.getName().endsWith( ".xml" ) ; }
	} );
 
	Statement stmt = con.createStatement();
	stmt.executeUpdate( "CREATE TABLE IF NOT EXISTS bgs_module_action_descr (`module` varchar(150) NOT NULL, `actionId` int(10) NOT NULL, `title` varchar(250) NOT NULL, PRIMARY KEY (`module`,`actionId`))" );
	stmt.executeUpdate( "TRUNCATE TABLE bgs_module_action_descr" );
	stmt.close();
 
	for( File file : files )
	{
		Document doc = XMLUtils.parseDocument( new FileInputStream( file ) );
 
		String name = file.getName();
		name = name.substring( 0, name.length() - 4 );
 
		LinkedList groups = new LinkedList();
		Map map = new HashMap();
		Element e = XMLUtils.selectElement( doc, "/actions" );
		StringBuilder sb = new StringBuilder();
 
		process( map, groups, e, sb );
 
		PreparedStatement ps = con.prepareStatement( "INSERT INTO bgs_module_action_descr (module, actionId, title) VALUES (?,?,?)" );
		ps.setString( 1, name );
 
		for( Map.Entry me : map.entrySet() )
		{
			print( name + " : " + me.getKey() + " : " + me.getValue() );
 
			ps.setInt( 2, me.getKey() );
			ps.setString( 3, me.getValue() );
			ps.executeUpdate();
		} 
 
		ps.close();
	}
}
 
private void process( Map map, LinkedList groups, Element e, StringBuilder sb )
{
	for( Element c : XMLUtils.elements( e.getChildNodes() ) )
	{
		if( "group".equals( c.getNodeName() ) )
		{
			groups.addLast( c.getAttribute( "title" ) );
 
			process( map, groups, c, sb );
 
			groups.removeLast();
		}
		else
		{
			int id = Utils.parseInt( c.getAttribute( "id" ) );
			String title = c.getAttribute( "title" );
 
			sb.setLength( 0 );
			for( String group : groups )
			{	
				sb.append( group );
				sb.append( "/" );
			}
 
			sb.append( title );
 
			map.put( id, sb.toString() );
		}
	}
}
Личные инструменты