summaryrefslogtreecommitdiff
path: root/orktrack/src/net/sf/oreka/orktrack/ProgramManager.java
blob: 6b8a54c32d12da40924728f725dbaa86981ffef0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

package net.sf.oreka.orktrack;

import java.util.ArrayList;
import java.util.List;

import net.sf.oreka.Direction;
import net.sf.oreka.HibernateManager;
import net.sf.oreka.persistent.RecProgram;
import net.sf.oreka.persistent.RecSegment;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ProgramManager {

	private Logger log = null;
	
	private static ProgramManager programManager = null;
	private ArrayList<RecProgram> recPrograms = new ArrayList<RecProgram>();
	
	private ProgramManager() {
		log = LogManager.getInstance().getPortLogger();;
	}
	
	public static ProgramManager instance() {
		
		if (programManager == null) {
			programManager = new ProgramManager();
		}
		return programManager;
	}
	
	public void load() {
		
		Session hbnSession = null;
		Transaction tx = null;
		
		try {
			hbnSession = HibernateManager.getSession();
			tx = hbnSession.beginTransaction();
			
			List progs = hbnSession.createQuery(
		    "from RecProgram as prg where prg.active=:active and prg.discarded=:discarded")
		    .setBoolean("active", true)
		    .setBoolean("discarded", false)
		    .list();
			
			recPrograms = new ArrayList<RecProgram>(progs);
			tx.commit();
		}
		catch (Exception e) {
			log.error("Could not load programs", e);
		}
		finally {
			hbnSession.close();
		}
	}
	
	public void addProgram(RecProgram prog) {
		
		recPrograms.add(prog);
	}
	
	public boolean filterSegmentAgainstAllPrograms(RecSegment seg, Session hbnSession) {
		
		boolean result = false;
		// Iterate over programs
		ArrayList<RecProgram> progs = recPrograms;
		for(int i=0; i<progs.size(); i++) {
			if(filterSegmentAgainstProgram(seg, progs.get(i), hbnSession)) {
				result = true;
			}
		}
		return result;
	}
	
	public boolean filterSegmentAgainstProgram(RecSegment seg, RecProgram prog, Session hbnSession) {
		
		boolean drop = false;
		boolean result = false;
		
		if (	prog.getDirection() != Direction.ALL &&
				seg.getDirection() != prog.getDirection()) {
			drop = true;
		}
		
		if (	!drop && prog.getMaxDuration() > 0 &&
				(seg.getDuration() > (prog.getMaxDuration()*1000))) {
			drop = true;
		}
		
		if (	!drop && prog.getMinDuration() > 0 &&
				(seg.getDuration() < (prog.getMinDuration()*1000))) {
			drop = true;
		}
		
		if (!drop && prog.getRandomPercent() != 0.0) {
			double randomNumber = java.lang.Math.random();
			if (randomNumber > prog.getRandomPercent()) {
				drop = true;
			}
		}
		
		if (	!drop && prog.getLocalParty().length() > 0 &&
				(!seg.getLocalParty().equals(prog.getLocalParty()))   ) {
			drop = true;
		}
		
		if (	!drop && prog.getRemoteParty().length() > 0 &&
				(!seg.getRemoteParty().equals(prog.getRemoteParty()))    ) {
			drop = true;
		}
			
		if (	!drop &&
				(prog.getTargetUser() != null) &&
				(seg.getUser().getId() != prog.getTargetUser().getId())   ) {
			drop = true;
		}
		
		if (	!drop &&
				(prog.getTargetPort() != null) &&
				(seg.getPort().getId() != prog.getTargetPort().getId())   ) {
			drop = true;
		}
		
		if ( 	!drop &&
				prog.getRecordedSoFar() < prog.getRecPerCycle() )
			drop = true;
		
		if(drop == false) {
			// All criteria have passed, the segment is accepted by the program
			prog.setRecordedSoFar(prog.getRecordedSoFar() + 1);
			hbnSession.update(prog);
			seg.getRecPrograms().add(prog);
			result = true;
		}
		return result;
	}
	
	public void filterTime() {
		;
	}
}