FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-code-example/HibernateDatabase/database/Utility.java at master · cherkavi/java-code-example · GitHub
cherkavi
/
java-code-example
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
0
Code
Issues
0
Pull requests
42
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-code-example
/
HibernateDatabase
/
database
/
Utility.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
197 lines (182 loc) · 6.96 KB
Breadcrumbs
java-code-example
/
HibernateDatabase
/
database
/
Utility.java
Copy path
File metadata and controls
executable file
·
197 lines (182 loc) · 6.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
pay
.
database
;
import
java
.
util
.
Random
;
import
org
.
hibernate
.
Session
;
import
org
.
hibernate
.
Transaction
;
import
org
.
hibernate
.
criterion
.
Restrictions
;
import
pay
.
database
.
wrap
.
Clients
;
import
pay
.
database
.
wrap
.
Satellite
;
public
class
Utility
{
protected
static
void
debug
(
String
value
){
System
.
out
.
print
(
"Utility"
);
System
.
out
.
print
(
" DEBUG: "
);
System
.
out
.
println
(
value
);
}
protected
static
void
error
(
String
value
){
System
.
out
.
print
(
"Utility"
);
System
.
out
.
print
(
" ERROR: "
);
System
.
out
.
println
(
value
);
}
/** ïîëó÷èòü ñëåäóþùèé êëþ÷ äîñòóïà ê ðåñóðñó Service äëÿ èíòåðíåò-ìàãàçèíà (Satellite)<br>
* ñãåíåðèðîâàòü íîâîå çíà÷åíèå SATELLITE.KEY_FOR_SERVICE
* @param satelliteName - óíèêàëüíîå èìÿ Ïàðòíåðà ( Satellite )
* @throws Exception - åñëè íå íàéäåíà çàïèñü ïî äàííîìó ïàðòíåðó
* @return êëþ÷, êîòîðûé äîëæåí áûòü ïîëó÷åí ïðè ñëåäóþùåì ñîåäèíåíèè ñ äàííûì Satellite
* */
public
static
String
getNextAccessKey
(
String
satelliteName
)
throws
Exception
{
Session
session
=
null
;
String
key
=
null
;
boolean
returnKey
=
true
;
try
{
session
=
Connector
.
getSession
();
// ïîëó÷èòü çàïèñü ïî óêàçàííîìó SatelliteId
Satellite
satellite
=((
Satellite
)
session
.
createCriteria
(
Satellite
.
class
).
add
(
Restrictions
.
like
(
"name"
,
satelliteName
)).
uniqueResult
());
// ñãåíåðèðîâàòü íîâûé óíèêàëüíûé êëþ÷
key
=
getUniqueChar
(
10
)+
Integer
.
toString
(
satellite
.
getId
());
// îáíîâèòü êëþ÷ â çàïèñè
satellite
.
setKey_for_service
(
key
);
Transaction
transaction
=
session
.
beginTransaction
();
session
.
save
(
satellite
);
transaction
.
commit
();
returnKey
=
true
;
// âåðíóòü êëþ÷
}
catch
(
Exception
ex
){
returnKey
=
false
;
}
finally
{
if
(
session
!=
null
){
Connector
.
closeSession
(
session
);
}
}
if
(
returnKey
==
false
){
throw
new
Exception
(
"getNextAccessKey error in create Next Access Key"
);
}
else
{
return
key
;
}
}
/** ïîëó÷èòü êëþ÷ äîñòóïà ê ðåñóðñó Service äëÿ èíòåðíåò-ìàãàçèíà (Satellite)<br>
* SATELLITE.KEY_FOR_SERVICE
* @param satelliteName - óíèêàëüíîå èìÿ Ïàðòíåðà ( Satellite )
* @throws Exception - åñëè íå íàéäåíà çàïèñü ïî äàííîìó ïàðòíåðó
* @return êëþ÷, êîòîðûé äîëæåí áûòü ïîëó÷åí ïðè ñëåäóþùåì ñîåäèíåíèè ñ äàííûì Satellite
* */
public
static
String
getAccessKey
(
String
satelliteName
){
Session
session
=
null
;
String
key
=
null
;
try
{
session
=
Connector
.
getSession
();
// ïîëó÷èòü çàïèñü ïî óêàçàííîìó SatelliteId
Satellite
satellite
=((
Satellite
)
session
.
createCriteria
(
Satellite
.
class
).
add
(
Restrictions
.
like
(
"name"
,
satelliteName
)).
uniqueResult
());
// ñãåíåðèðîâàòü íîâûé óíèêàëüíûé êëþ÷
key
=
satellite
.
getKey_for_service
();
}
catch
(
Exception
ex
){
error
(
"getAccessKey:"
+
ex
.
getMessage
());
}
finally
{
if
(
session
!=
null
){
Connector
.
closeSession
(
session
);
}
}
return
key
;
}
/** ïðîâåðèòü âàëèäíîñòü êëþ÷à (KEY_FOR_SERVICE) äîñòóïà, êîòîðûé ïðèñëàë Satellite
* @param satelliteName èìÿ Satellite
* @param accessKey - êëþ÷, êîòîðûé äîëæåí áûòü ñîõðàíåí â áàçå êàê êëþ÷ äîñòóïà äëÿ äàííîãî Satellite
* */
public
static
boolean
checkAccesKeyBySatelliteId
(
String
satelliteName
,
String
accessKey
){
Session
session
=
null
;
boolean
return_value
=
false
;
try
{
session
=
Connector
.
getSession
();
// ïîëó÷èòü çàïèñü ïî óêàçàííîìó SatelliteId
Satellite
satellite
=((
Satellite
)
session
.
createCriteria
(
Satellite
.
class
).
add
(
Restrictions
.
like
(
"name"
,
satelliteName
)).
uniqueResult
());
return_value
=
satellite
.
getKey_for_service
().
equals
(
accessKey
);
}
catch
(
Exception
ex
){
error
(
"checkAccessKeyBySatelliteId Exception:"
+
ex
.
getMessage
());
}
finally
{
if
(
session
!=
null
){
Connector
.
closeSession
(
session
);
}
}
return
return_value
;
}
/** ñãåíåðèðîâàòü óíèêàëüíûé êëþ÷ (áåç ñîõðàíåíèÿ â áàçå ) êëèåíòà äëÿ Satellite <br>
* c êîòîðûì (êëþ÷åì) äàííûé êëèåíò ñìîæåò ñàìîñòîÿòåëüíî "çàéòè" ê íàì íà HTML ðåñóðñ */
public
static
String
generateUniqueKeyForSatellite
(
String
satelliteName
){
try
{
return
getUniqueChar
(
10
)+
Integer
.
toString
(
getSatelliteIdFromSatelliteName
(
satelliteName
));
}
catch
(
Exception
ex
){
return
""
;
}
}
/** ñîçäàòü óíèêàëüíûé êîä äëÿ êëèåíòà - çàïèñü â òàáëèöå CLIENTS
* @param satelliteName - óíèêàëüíîå èìÿ ïàðòíåðà (Satellite)
* @param unique_key_satellite - óíèêàëüíûé êëþ÷ äëÿ äàííîãî êëèåíòà â ìàñøòàáå Satellite
* @param unique_key_service - óíèêàëüíûé êëþ÷ äëÿ äàííîãî êëèåíòà â ìàñøòàáå Service
* @param returnPath - ïóòü, ïî êîòîðîìó íóæíî íàïðàâèòü äàííîãî êëèåíòà äëÿ âîçâðàòà åãî íà ðåñóðñ Satellite
* @return true - åñëè îáíîâëåíèå äàííûõ ïðîøëî óñïåøíî
*/
public
static
boolean
createClients
(
String
satelliteName
,
String
unique_key_satellite
,
String
unique_key_service
,
String
returnPath
){
boolean
return_value
=
false
;
Session
session
=
null
;
try
{
session
=
Connector
.
getSession
();
Transaction
transaction
=
session
.
beginTransaction
();
// create Clients
int
satelliteId
=
getSatelliteIdFromSatelliteName
(
satelliteName
);
Clients
newClients
=
new
Clients
();
newClients
.
setId_client_state
(
0
);
newClients
.
setId_satellite
(
satelliteId
);
newClients
.
setReturn_url
(
returnPath
);
newClients
.
setUnique_key_satellite
(
unique_key_satellite
);
newClients
.
setUnique_key_service
(
unique_key_service
);
session
.
save
(
newClients
);
transaction
.
commit
();
return_value
=
true
;
}
catch
(
Exception
ex
){
error
(
"createClients Exception:"
+
ex
.
getMessage
());
}
finally
{
if
(
session
!=
null
){
Connector
.
closeSession
(
session
);
}
}
return
return_value
;
}
/** ïîëó÷èòü óíèêàëüíûé êîä â ìàñøòàáå áàçû äàííûõ(òàáëèöû) Satellite */
public
static
int
getSatelliteIdFromSatelliteName
(
String
satelliteName
)
throws
Exception
{
int
return_value
=(-
1
);
Session
session
=
null
;
try
{
session
=
Connector
.
getSession
();
return_value
=((
Satellite
)
session
.
createCriteria
(
Satellite
.
class
).
add
(
Restrictions
.
like
(
"name"
,
satelliteName
)).
uniqueResult
()).
getId
();
}
catch
(
Exception
ex
){
error
(
"getSatelliteIdFromSatelliteName is not finded"
);
}
finally
{
if
(
session
!=
null
){
Connector
.
closeSession
(
session
);
}
}
// return result
if
(
return_value
==(-
1
)){
throw
new
Exception
(
"Satellite Id is not finded: "
+
satelliteName
);
}
else
{
return
return_value
;
}
}
public
final
static
String
hexChars
[] = {
"0"
,
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
"A"
,
"B"
,
"C"
,
"D"
,
"E"
,
"F"
};
/** ñãåíåðèðîâàòü ñëó÷àéíóþ ïîñëåäîâàòåëüíîñòü èç Hex ÷èñåë, óêàçàííîé äëèííû
* @param count - äëèííà ñëó÷àéíîé ïîñëåäîâàòåëüíîñòè, êîòîðóþ íåîáõîäèìî ïîëó÷èòü
* */
public
static
String
getUniqueChar
(
int
count
){
StringBuffer
return_value
=
new
StringBuffer
();
Random
random
=
new
java
.
util
.
Random
();
int
temp_value
;
for
(
int
counter
=
0
;
counter
<
count
;
counter
++){
temp_value
=
random
.
nextInt
(
hexChars
.
length
);
return_value
.
append
(
hexChars
[
temp_value
]);
}
return
return_value
.
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL