Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.schema; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.entities.Account; 19 : import com.google.gerrit.entities.RefNames; 20 : import com.google.gerrit.server.git.meta.VersionedMetaData; 21 : import java.io.IOException; 22 : import org.eclipse.jgit.errors.ConfigInvalidException; 23 : import org.eclipse.jgit.lib.CommitBuilder; 24 : import org.eclipse.jgit.lib.Config; 25 : 26 : /** Preferences for user accounts during schema migrations. */ 27 : class VersionedAccountPreferences extends VersionedMetaData { 28 : static final String PREFERENCES = "preferences.config"; 29 : 30 : static VersionedAccountPreferences forUser(Account.Id id) { 31 0 : return new VersionedAccountPreferences(RefNames.refsUsers(id)); 32 : } 33 : 34 : static VersionedAccountPreferences forDefault() { 35 0 : return new VersionedAccountPreferences(RefNames.REFS_USERS_DEFAULT); 36 : } 37 : 38 : private final String ref; 39 : private Config cfg; 40 : 41 0 : protected VersionedAccountPreferences(String ref) { 42 0 : this.ref = ref; 43 0 : } 44 : 45 : @Override 46 : protected String getRefName() { 47 0 : return ref; 48 : } 49 : 50 : Config getConfig() { 51 0 : return cfg; 52 : } 53 : 54 : @Override 55 : protected void onLoad() throws IOException, ConfigInvalidException { 56 0 : cfg = readConfig(PREFERENCES); 57 0 : } 58 : 59 : @Override 60 : protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { 61 0 : if (Strings.isNullOrEmpty(commit.getMessage())) { 62 0 : commit.setMessage("Updated preferences\n"); 63 : } 64 0 : saveConfig(PREFERENCES, cfg); 65 0 : return true; 66 : } 67 : }