summaryrefslogtreecommitdiffstats
path: root/Dozentenmodulserver/src/server/ServerMethod.java
blob: aa4dcae84af3b1604f53a3b12a89da7161e51116 (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package server;
import java.math.BigInteger;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import Models.User;

import sql.SQL;

@SuppressWarnings("serial")
public class ServerMethod extends UnicastRemoteObject implements ServerInterface
{
    protected static String          m_strName;
    SQL sql=new SQL();
    Connection con=sql.getConnection();

    public ServerMethod() throws RemoteException
    {
        super(); // call base class constructor
    }
    
   
            
    public static void main(String argv[])
    {
        try
        {
        	LocateRegistry.createRegistry(9999);
        	
            m_strName = "TheRMIExample";
            System.out.println("Server: Registering RMIExampleImpl as \"" + m_strName +"\"");
            //System.setSecurityManager(new RMISecurityManager());
            ServerMethod Example = new ServerMethod();
            Naming.rebind("rmi://141.79.128.121:9999/"+m_strName, Example);
            System.out.println("Server: Ready...");
        }
        catch (Exception e)
        {
            System.out.println("Server: Failed to register RMIExampleImpl: " + e);
        }
    }



	@Override
	public User getFtpUser() throws RemoteException {
		User user=new User();
		user.setUsername(UUID.randomUUID().toString().substring(0, 8));
		user.setPass(getEncodedSha1Sum(UUID.randomUUID().toString().substring(0, 8)));
		user.setPath("/srv/openslx/nfs/temp");
		SQL sql=new SQL();
		Connection con=sql.getConnection();
		sql.writeFTPUser(con, user.getUsername(), user.getPass());
		return user;
	}



	@Override
	public int DeleteFtpUser(String user) throws RemoteException {
		
		
		int ret = sql.DeleteUser(con, user);
		return ret;
	}

	public String getEncodedSha1Sum(String key) {
	    try {
	        MessageDigest md = MessageDigest.getInstance( "SHA1" );
	        md.update( key.getBytes() );
	        return new BigInteger(1, md.digest()).toString(16);
	    }
	    catch (NoSuchAlgorithmException e) {
	        // handle error case to taste
	    }
		return null;
	}

	
	
	public ArrayList<String> getImages(){
		
		

		ResultSet rs= sql.getImage(con);
		ArrayList<String> al=new ArrayList<String>();
		try {
			while(rs.next())
			{
						
				al.add(rs.getString(1));				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return al;
	}
	
	public String getPathOfImage(String name){
		
		String path = null;

		ResultSet rs= sql.getPathOfImage(con, name);
		
		try {
			while(rs.next())
			{
						
				path=rs.getString(1);				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return path;
	}

	@Override
	public boolean writeVLdata(String imagename, String firstname,
			String lastname, String university, String Mail, String Tel,
			String Fak, boolean license, boolean internet, int ram, int cpu)
			throws RemoteException {
		String login="tete";
		
		int pk_institution=sql.setInstitution(con, university);
		
		int pk_person=sql.setPerson(con, login, lastname, firstname, Mail, new Date(), pk_institution);
		
		sql.setImageData(con, pk_person, license, internet, cpu, ram, imagename);
		
		// TODO Auto-generated method stub
		return true;
	}
	



	@Override
	public List<Object[]> getImageList() throws RemoteException {
		
		ResultSet resWith=sql.getImageListWithLectures(con);
		ResultSet resWithout=sql.getImageListWithoutLectures(con);
		 
		try {
			List<Object[]> listWith = ResSetToObject(resWith);
			List<Object[]> listWithout=ResSetToObject(resWithout);
			listWith.addAll(listWith.size(), listWithout);
			
			return listWith;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return null;
	}

	public List<Object[]> ResSetToObject(ResultSet res) throws SQLException
	{
		
		ResultSetMetaData rsmd = res.getMetaData();
		List<Object[]> list=new ArrayList<>();
		while(res.next()){
			 Object[] objects = new Object[rsmd.getColumnCount()];
			 // tanks to umit ozkan for the bug fix!
			 for(int i=0;i<rsmd.getColumnCount();i++){
			  objects[i]=res.getObject(i+1);
			  }
			 list.add(objects);
			 
	}
		return list;
	}



	@Override
	public List<String> getAllOS() throws RemoteException {
		List<String> list=new ArrayList<>();
		ResultSet rs=sql.getAllOS(con);
		try {
			while(rs.next())
			{
				list.add(rs.getString("name"));
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}



	@Override
	public Map<String,String> getPersonData(String Vorname, String Nachname)
			throws RemoteException {
		Map<String,String> map=new HashMap<String, String>();
		System.out.println(Vorname+Nachname);
		ResultSet rs=sql.getPersonData(con, Vorname, Nachname);
		try {
			while(rs.next())
			{
				
				map.put("mail", rs.getString("mail"));
				map.put("Nachname", rs.getString("Nachname"));
				map.put("Vorname", rs.getString("Vorname"));
				map.put("Hochschule", rs.getString("name"));
				map.put("tel", "009909");
				map.put("fak", "E+I");
				
			}
			return map;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public boolean writeLecturedata(String name, String shortdesc, String desc,
			Date start, Date end, boolean isActive, String imagename,
			String firstname, String lastname, String university, String Mail,
			String Tel, String Fak) throws RemoteException {
		int pk_image = 0;
		String login="tete";
		int imageversion = 0;
		int pk_institution=sql.setInstitution(con, university);
		int pk_person=sql.setPerson(con, login, lastname, firstname, Mail, new Date(), pk_institution);
		System.out.println(imagename);
		ResultSet image=sql.getImageIDandVersion(con, imagename);

			try {
				while(image.next())
				{
				pk_image=image.getInt("GUID_imageID");
				imageversion=image.getInt("imageVersion");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			sql.setLectureData(con, pk_person, pk_image, imageversion, name, desc, shortdesc, start, end, isActive);
		
		return false;
	}
}