/
MappedInt.scala
291 lines (230 loc) · 9.6 KB
/
MappedInt.scala
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2006-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.liftweb
package mapper
import java.sql.{ResultSet, Types}
import java.lang.reflect.Method
import net.liftweb.common._
import net.liftweb.util._
import Helpers._
import java.util.Date
import net.liftweb.http._
// import net.liftweb.widgets.autocomplete._
import net.liftweb.json._
import net.liftweb.http.jquery.{JqSHtml}
import scala.xml.NodeSeq
import js._
/**
* Warning: Do not use unnamed Enumerations with 2.8.1 as this will cause too many items to be displayed in the dropdown.
*
* See https://issues.scala-lang.org/browse/SI-3687 for details
*/
abstract class MappedEnum[T<:Mapper[T], ENUM <: Enumeration](val fieldOwner: T, val enum: ENUM) extends MappedField[ENUM#Value, T] {
private var data: ENUM#Value = defaultValue
private var orgData: ENUM#Value = defaultValue
def defaultValue: ENUM#Value = enum.values.iterator.next
def dbFieldClass = classOf[ENUM#Value]
/**
* Get the JDBC SQL Type for this field
*/
def targetSQLType = Types.BIGINT
protected def i_is_! = data
protected def i_was_! = orgData
/**
* Called after the field is saved to the database
*/
override protected[mapper] def doneWithSave() {
orgData = data
}
protected def real_i_set_!(value: ENUM#Value): ENUM#Value = {
if (value != data) {
data = value
dirty_?(true)
}
data
}
override def readPermission_? = true
override def writePermission_? = true
def real_convertToJDBCFriendly(value: ENUM#Value): Object = new java.lang.Integer(value.id)
def toInt = is.id
def fromInt(in: Int): ENUM#Value = enum(in)
def jdbcFriendly(field: String) = new java.lang.Integer(toInt)
override def jdbcFriendly = new java.lang.Integer(toInt)
def asJsExp: JsExp = JE.Num(is.id)
def asJsonValue: Box[JsonAST.JValue] = Full(JsonAST.JInt(is.id))
override def setFromAny(in: Any): ENUM#Value = {
in match {
case JsonAST.JInt(bi) => this.set(fromInt(bi.intValue))
case n: Int => this.set(fromInt(n))
case n: Long => this.set(fromInt(n.toInt))
case n: Number => this.set(fromInt(n.intValue))
case (n: Number) :: _ => this.set(fromInt(n.intValue))
case Some(n: Number) => this.set(fromInt(n.intValue))
case Full(n: Number) => this.set(fromInt(n.intValue))
case None | Empty | Failure(_, _, _) => this.set(defaultValue)
case (s: String) :: _ => this.set(fromInt(Helpers.toInt(s)))
case vs: ENUM#Value => this.set(vs)
case null => this.set(defaultValue)
case s: String => this.set(fromInt(Helpers.toInt(s)))
case o => this.set(fromInt(Helpers.toInt(o)))
}
}
protected def i_obscure_!(in : ENUM#Value) = defaultValue
private def st(in: ENUM#Value) {
data = in
orgData = in
}
def buildSetActualValue(accessor: Method, data: AnyRef, columnName: String) : (T, AnyRef) => Unit =
(inst, v) => doField(inst, accessor, {case f: MappedEnum[T, ENUM] => f.st(if (v eq null) defaultValue else fromInt(Helpers.toInt(v.toString)))})
def buildSetLongValue(accessor: Method, columnName: String): (T, Long, Boolean) => Unit =
(inst, v, isNull) => doField(inst, accessor, {case f: MappedEnum[T, ENUM] => f.st(if (isNull) defaultValue else fromInt(v.toInt))})
def buildSetStringValue(accessor: Method, columnName: String): (T, String) => Unit =
(inst, v) => doField(inst, accessor, {case f: MappedEnum[T, ENUM] => f.st(if (v eq null) defaultValue else fromInt(Helpers.toInt(v)))})
def buildSetDateValue(accessor: Method, columnName: String): (T, Date) => Unit =
(inst, v) => doField(inst, accessor, {case f: MappedEnum[T, ENUM] => f.st(if (v eq null) defaultValue else fromInt(Helpers.toInt(v)))})
def buildSetBooleanValue(accessor: Method, columnName: String): (T, Boolean, Boolean) => Unit =
(inst, v, isNull) => doField(inst, accessor, {case f: MappedEnum[T, ENUM] => f.st(defaultValue)})
/**
* Given the driver type, return the string required to create the column in the database
*/
def fieldCreatorString(dbType: DriverType, colName: String): String = colName + " " + dbType.enumColumnType + notNullAppender()
/*
Mapper dependency on Widgets is the wrong order. There should be a trait in Widgets that's
mixed into this class that provides autocomplete. dpp 2009/12/01
/**
* Whether or not to use autocomplete in toForm
*/
def autocomplete_? = false
*/
/**
* Build a list for the select. Return a tuple of (String, String) where the first string
* is the id.string of the Value and the second string is the Text name of the Value.
*/
def buildDisplayList: List[(Int, String)] = enum.values.toList.map(a => (a.id, a.toString))
/**
* Create an input field for the item
*/
override def _toForm: Box[NodeSeq] =
/*
if (autocomplete_?)
Full(AutoComplete.autocompleteObj[Int](buildDisplayList, Full(toInt),
v => this.set(fromInt(v))))
else
*/
Full(SHtml.selectObj[Int](buildDisplayList, Full(toInt),
v => this.set(fromInt(v))))
}
abstract class MappedIntIndex[T<:Mapper[T]](owner : T) extends MappedInt[T](owner) with IndexedField[Int] {
override def writePermission_? = false // not writable
override def dbPrimaryKey_? = true
override def defaultValue = -1
def defined_? = i_is_! != defaultValue
override def dbIndexFieldIndicatesSaved_? = {i_is_! != defaultValue}
def makeKeyJDBCFriendly(in : Int) = new java.lang.Integer(in)
def convertKey(in : String): Box[Int] = {
if (in eq null) Empty
try {
val what = if (in.startsWith(name + "=")) in.substring((name + "=").length) else in
Full(Integer.parseInt(what))
} catch {
case _ => Empty
}
}
override def dbDisplay_? = false
def convertKey(in : Int): Box[Int] = {
if (in < 0) Empty
else Full(in)
}
def convertKey(in : Long): Box[Int] = {
if (in < 0 || in > Integer.MAX_VALUE) Empty
else Full(in.asInstanceOf[Int])
}
def convertKey(in : AnyRef): Box[Int] = {
if ((in eq null) || (in eq None)) None
try {
convertKey(in.toString)
} catch {
case _ => Empty
}
}
override def fieldCreatorString(dbType: DriverType, colName: String): String = colName + " " + dbType.integerIndexColumnType + notNullAppender()
}
abstract class MappedInt[T<:Mapper[T]](val fieldOwner: T) extends MappedField[Int, T] {
private var data: Int = defaultValue
private var orgData: Int = defaultValue
def defaultValue = 0
def dbFieldClass = classOf[Int]
/**
* Get the JDBC SQL Type for this field
*/
def targetSQLType = Types.INTEGER
protected def i_is_! = data
protected def i_was_! = orgData
/**
* Called after the field is saved to the database
*/
override protected[mapper] def doneWithSave() {
orgData = data
}
def asJsExp: JsExp = JE.Num(is)
def asJsonValue: Box[JsonAST.JValue] = Full(JsonAST.JInt(is))
protected def real_i_set_!(value : Int) : Int = {
if (value != data) {
data = value
this.dirty_?( true)
}
data
}
override def readPermission_? = true
override def writePermission_? = true
def +(in: Int): Int = is + in
def real_convertToJDBCFriendly(value: Int): Object = new java.lang.Integer(value)
def jdbcFriendly(field : String) = new java.lang.Integer(is)
override def setFromAny(in: Any): Int = {
in match {
case n: Int => this.set(n)
case JsonAST.JInt(bigint) => this.set(bigint.intValue)
case n: Number => this.set(n.intValue)
case (n: Number) :: _ => this.set(n.intValue)
case Some(n: Number) => this.set(n.intValue)
case Full(n: Number) => this.set(n.intValue)
case None | Empty | Failure(_, _, _) => this.set(0)
case (s: String) :: _ => this.set(toInt(s))
case null => this.set(0)
case s: String => this.set(toInt(s))
case o => this.set(toInt(o))
}
}
protected def i_obscure_!(in : Int) = 0
private def st(in: Int) {
data = in
orgData = in
}
def buildSetActualValue(accessor: Method, v: AnyRef, columnName: String): (T, AnyRef) => Unit =
(inst, v) => doField(inst, accessor, {case f: MappedInt[T] => f.st(toInt(v))})
def buildSetLongValue(accessor: Method, columnName: String): (T, Long, Boolean) => Unit =
(inst, v, isNull) => doField(inst, accessor, {case f: MappedInt[T] => f.st(if (isNull) 0 else v.toInt)})
def buildSetStringValue(accessor: Method, columnName: String): (T, String) => Unit =
(inst, v) => doField(inst, accessor, {case f: MappedInt[T] => f.st(toInt(v))})
def buildSetDateValue(accessor: Method, columnName: String): (T, Date) => Unit =
(inst, v) => doField(inst, accessor, {case f: MappedInt[T] => f.st(toInt(v))})
def buildSetBooleanValue(accessor: Method, columnName: String): (T, Boolean, Boolean) => Unit =
(inst, v, isNull) => doField(inst, accessor, {case f: MappedInt[T] => f.st(if (isNull || !v) 0 else 1)})
/**
* Given the driver type, return the string required to create the column in the database
*/
def fieldCreatorString(dbType: DriverType, colName: String): String = colName + " " + dbType.integerColumnType + notNullAppender()
}