001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.repository.query.expression; 017 018import org.ametys.core.user.UserIdentity; 019import org.ametys.plugins.repository.RepositoryConstants; 020 021/** 022 * Constructs an {@link Expression} corresponding to a user comparison store as a composite with login and id of population. 023 */ 024public class UserExpression implements Expression 025{ 026 private UserIdentity _user; 027 private Operator _operator; 028 private String _metadata; 029 private boolean _unversioned; 030 031 /** 032 * Creates the comparison Expression. 033 * @param metadata the metadata path 034 * @param operator the operator to make the comparison 035 * @param value the user identity as login#population. 036 */ 037 public UserExpression(String metadata, Operator operator, String value) 038 { 039 this(metadata, operator, UserIdentity.stringToUserIdentity(value), false); 040 } 041 042 /** 043 * Creates the comparison Expression. 044 * @param metadata the metadata path 045 * @param operator the operator to make the comparison 046 * @param value the user identity as login#population. 047 * @param unversioned true if the metadata is unversioned, false otherwise. 048 */ 049 public UserExpression(String metadata, Operator operator, String value, boolean unversioned) 050 { 051 this(metadata, operator, UserIdentity.stringToUserIdentity(value), unversioned); 052 } 053 054 /** 055 * Creates the comparison Expression. 056 * @param metadata the metadata path 057 * @param operator the operator to make the comparison 058 * @param userIdentity the user identity (login and population) 059 */ 060 public UserExpression(String metadata, Operator operator, UserIdentity userIdentity) 061 { 062 this(metadata, operator, userIdentity, false); 063 } 064 065 /** 066 * Creates the comparison Expression. 067 * @param metadata the metadata path 068 * @param operator the operator to make the comparison 069 * @param userIdentity the user identity (login and population) 070 * @param unversioned true if the metadata is unversioned, false otherwise. 071 */ 072 public UserExpression(String metadata, Operator operator, UserIdentity userIdentity, boolean unversioned) 073 { 074 _user = userIdentity; 075 _metadata = metadata; 076 _unversioned = unversioned; 077 _operator = operator; 078 } 079 080 @Override 081 public String build() 082 { 083 Expression expr = new AndExpression(new UserLoginExpression(_metadata, _operator, _user.getLogin(), _unversioned), new UserPopulationExpression(_metadata, _operator, _user.getPopulationId(), _unversioned)); 084 return expr.build(); 085 } 086 087 /** 088 * Internal class to create a expression based on a user's login comparison 089 */ 090 class UserLoginExpression implements Expression 091 { 092 private String _uleValue; 093 private Operator _uleOperator; 094 095 private String _uleMetadata; 096 private String _ulePath; 097 private boolean _uleUnversioned; 098 099 /** 100 * Construction an expression to test a user login 101 * @param metadata The path to metadata 102 * @param operator The operator 103 * @param value The login value 104 * @param unversioned unversioned true if the metadata is unversioned, false otherwise. 105 */ 106 public UserLoginExpression(String metadata, Operator operator, String value, boolean unversioned) 107 { 108 _uleValue = value; 109 _uleOperator = operator; 110 111 int index = metadata.lastIndexOf('/'); 112 boolean isComposite = index != -1; 113 _ulePath = isComposite ? metadata.substring(0, index) : null; 114 _uleMetadata = isComposite ? metadata.substring(index + 1) : metadata; 115 _uleUnversioned = unversioned; 116 } 117 118 @Override 119 public String build() 120 { 121 StringBuilder buff = new StringBuilder(); 122 123 if (_uleUnversioned) 124 { 125 buff.append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(":unversioned/"); 126 } 127 128 if (_ulePath != null) 129 { 130 String[] parts = _ulePath.split("/"); 131 for (String part : parts) 132 { 133 if ("*".equals(part)) 134 { 135 buff.append(part).append('/'); 136 } 137 else 138 { 139 buff.append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append(part).append('/'); 140 } 141 142 } 143 } 144 145 buff.append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append(_uleMetadata).append('/').append('@').append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append("login"); 146 buff.append(" " + _uleOperator); 147 buff.append(" '" + _uleValue + "'"); 148 149 return buff.toString(); 150 } 151 } 152 153 /** 154 * Internal class to create a expression based on a user's population comparison 155 */ 156 class UserPopulationExpression implements Expression 157 { 158 private String _upeValue; 159 private Operator _upeOperator; 160 161 private String _upeMetadata; 162 private String _upePath; 163 private boolean _upeUnversioned; 164 165 /** 166 * Construction an expression to test a user population 167 * @param metadata The path to metadata 168 * @param operator The operator 169 * @param value The population's id 170 * @param unversioned unversioned true if the metadata is unversioned, false otherwise. 171 */ 172 public UserPopulationExpression(String metadata, Operator operator, String value, boolean unversioned) 173 { 174 _upeValue = value; 175 _upeOperator = operator; 176 177 int index = metadata.lastIndexOf('/'); 178 boolean isComposite = index != -1; 179 _upePath = isComposite ? metadata.substring(0, index) : null; 180 _upeMetadata = isComposite ? metadata.substring(index + 1) : metadata; 181 _upeUnversioned = unversioned; 182 } 183 184 @Override 185 public String build() 186 { 187 StringBuilder buff = new StringBuilder(); 188 189 if (_upeUnversioned) 190 { 191 buff.append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(":unversioned/"); 192 } 193 194 if (_upePath != null) 195 { 196 String[] parts = _upePath.split("/"); 197 for (String part : parts) 198 { 199 if ("*".equals(part)) 200 { 201 buff.append(part).append('/'); 202 } 203 else 204 { 205 buff.append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append(part).append('/'); 206 } 207 208 } 209 } 210 211 buff.append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append(_upeMetadata).append('/').append('@').append(RepositoryConstants.NAMESPACE_PREFIX).append(':').append("population"); 212 buff.append(" " + _upeOperator); 213 buff.append(" '" + _upeValue + "'"); 214 215 return buff.toString(); 216 } 217 } 218 219 220}