In fact, both model and record belong to map, but in some cases, you still need to convert the attributes and values in the model or record into a map key-value pair, because the field and value need to be transferred during buildcondition, in order to share a method and unify it, the model and record are all converted into a map and passed to buildcondition, and such conversion is also required elsewhere, for example, if you directly pass a model to the front-end page and it seems that you cannot obtain the attributes of the model, you need to convert it yourself! Therefore, we have written these two methods.
1. Convert the model to map
/*** Convert the model class to map modeltomap ** @ Param parameter description * @ return returns the object * @ exception object */public static Map <string, object> modeltomap (Model <?> Model) {Map <string, Object> map = new hashmap <string, Object> (); string [] names = model. getattrnames (); For (string STR: names) {map. put (STR, model. get (STR);} return map ;}
2. convert a record to a map
/*** Convert a record to map recordtomap ** @ Param parameter description * @ return returns the object * @ exception object */public static Map <string, object> recordtomap (record) {Map <string, Object> map = new hashmap <string, Object> (); If (record! = NULL) {string [] Columns = record. getcolumnnames (); For (string Col: columns) {map. put (COL, record. get (COL) ;}} return map ;}
3. Check whether the object is empty.
/*** Determines whether each object in the object or object array is null: the object is null, And the Character Sequence length is 0, set class and map to empty ** @ Param OBJ * @ return */@ suppresswarnings ("unchecked") public static Boolean isnullorempty (Object OBJ) {If (OBJ = NULL) {return true;} else if (OBJ instanceof string & (obj. equals ("") {return true;} else if (OBJ instanceof short & (short) OBJ ). repeated value () = 0) {return true;} else if (OBJ instanceof integer & (integer) ob J ). intvalue () = 0) {return true;} else if (OBJ instanceof double & (double) OBJ ). doublevalue () = 0) {return true;} else if (OBJ instanceof float & (float) OBJ ). floatvalue () = 0) {return true;} else if (OBJ instanceof long & (long) OBJ ). longvalue () = 0) {return true;} else if (OBJ instanceof Boolean &&! (Boolean) OBJ) {return true;} else if (OBJ instanceof Collection & (Collection) OBJ ). isempty () {return true;} else if (OBJ instanceof map & (MAP) OBJ ). isempty () {return true;} else if (OBJ instanceof object [] & (object []) OBJ ). length = 0) {return true;} return false ;}
Several methods used in the tool class for generating dynamic conditions using jfinal