public UserProfile getUserProfileByFname (final IPerson person, final String profileFname) {
Tuple<String, String> key = null;
final Cache<Tuple<String, String>, UserProfile> profileCache = getProfileImportExportCache();
if (profileCache != null) {
key = new Tuple<String, String>(person.getUserName(), profileFname);
final UserProfile profile = profileCache.getIfPresent(key);
if (profile != null) {
return profile;
}
}
logger.debug("Getting profile {} for user {}", profileFname, person.getID());
final int userId = person.getID();
final UserProfile userProfile = jdbcOperations.execute(new ConnectionCallback<UserProfile>() {
@Override
public UserProfile doInConnection(Connection con) throws SQLException, DataAccessException {
String query = "SELECT USER_ID, PROFILE_ID, PROFILE_NAME, DESCRIPTION, " +
"LAYOUT_ID, STRUCTURE_SS_ID, THEME_SS_ID FROM UP_USER_PROFILE WHERE " +
"USER_ID=? AND PROFILE_FNAME=?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, userId);
pstmt.setString(2, profileFname);
try {
logger.debug("getUserProfileByFname(): {} userId: {} profileFname: {}", query, userId, profileFname);
ResultSet rs = pstmt.executeQuery();
try {
if (rs.next()) {
int profileId = rs.getInt(2);
String profileName = rs.getString(3);
String profileDesc = rs.getString(4);
int layoutId = rs.getInt(5);
if (rs.wasNull()) {
layoutId = 0;
}
int structSsId = rs.getInt(6);
if (rs.wasNull()) {
// This is probably a data issue and probably an export operation; defer to the system user...
if (!person.equals(getSystemUser())) {
structSsId = getSystemProfileByFname(profileFname).getStructureStylesheetId();
} else {
String msg = "The system user profile has no structure stylesheet Id.";
throw new IllegalStateException(msg);
}
}
int themeSsId = rs.getInt(7);
if (rs.wasNull()) {
// This is probably a data issue and probably an export operation; defer to the system user...
if (!person.equals(getSystemUser())) {
themeSsId = getSystemProfileByFname(profileFname).getThemeStylesheetId();
} else {
String msg = "The system user profile has no theme stylesheet Id.";
throw new IllegalStateException(msg);
}
}
UserProfile userProfile = new UserProfile(profileId, profileFname, profileName, profileDesc, layoutId, structSsId, themeSsId);
final Locale[] userLocales = localeStore.getUserLocales(person);
userProfile.setLocaleManager(new LocaleManager(person, userLocales));
return userProfile;
}
/* Try to copy the template profile. */
logger.debug("Copying template profile {} to user {}", profileFname, person.getID());
rs.close();
pstmt.close();
pstmt = con.prepareStatement("SELECT USER_DFLT_USR_ID FROM UP_USER WHERE USER_ID=?");
pstmt.setInt(1, person.getID());
rs = pstmt.executeQuery();
if(rs.next()) {
int defaultProfileUser = rs.getInt(1);
if (rs.wasNull()) {
throw new RuntimeException("Need to clone the '" + profileFname + "' profile from template user for " + person + " but they have no template user");
}
IPerson defaultProfilePerson = new PersonImpl();
defaultProfilePerson.setID(defaultProfileUser);
if(defaultProfilePerson.getID() != person.getID()) {
UserProfile templateProfile = getUserProfileByFname(defaultProfilePerson,profileFname);
if(templateProfile != null) {
UserProfile newUserProfile = new UserProfile(templateProfile);
final Locale[] userLocales = localeStore.getUserLocales(person);
newUserProfile.setLayoutId(0);
newUserProfile = addUserProfile(person,newUserProfile);
newUserProfile.setLocaleManager(new LocaleManager(person, userLocales));
return newUserProfile;
}
}
}