<commit_before>/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * 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 th.or.nectec.tanrabad.survey.presenter.authen;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.trb.authen.client.TRBAuthenUtil;
import org.trb.authen.client.TRBCallback;
import org.trb.authen.model.UserProfile;
import th.or.nectec.tanrabad.domain.organization.OrganizationRepository;
import th.or.nectec.tanrabad.domain.user.UserRepository;
import th.or.nectec.tanrabad.entity.Organization;
import th.or.nectec.tanrabad.entity.User;
import th.or.nectec.tanrabad.survey.R;
import th.or.nectec.tanrabad.survey.presenter.AccountUtils;
import th.or.nectec.tanrabad.survey.presenter.TanrabadActivity;
import th.or.nectec.tanrabad.survey.repository.BrokerOrganizationRepository;
import th.or.nectec.tanrabad.survey.repository.BrokerUserRepository;
import th.or.nectec.tanrabad.survey.utils.android.CookieUtils;

@SuppressLint("SetJavaScriptEnabled")
public class AuthenActivity extends TanrabadActivity {

    private WebView webView;
    private WebChromeClient webChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            setProgress(newProgress * 1000);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authen);

        CookieUtils.clearCookies(this);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new AuthenWebViewClient());
        webView.setWebChromeClient(webChromeClient);
        webView.getSettings().setJavaScriptEnabled(true);
        loadAuthenticationPage();
    }

    private void loadAuthenticationPage() {
        try {
            String authenUrl = TRBAuthenUtil.getInstance().getAuthorizationUri();
            webView.loadUrl(authenUrl);
        } catch (Exception expect) {
            expect.printStackTrace();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class AuthenWebViewClient extends WebViewClient {

        private static final String PARAM_CODE = "code";
        private static final String AUTHORIZED_CALLBACK_URL = "http://localhost/?";

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith(AUTHORIZED_CALLBACK_URL)) {
                Uri uri = Uri.parse(url);
                String code = uri.getQueryParameter(PARAM_CODE);
                TRBAuthenUtil.getInstance().onAuthorizationResult(code, new AuthenticationCallback());
                webView.stopLoading();
                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onReceivedSslError(WebView view, @NonNull SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    }

    private class AuthenticationCallback implements TRBCallback {

        private OrganizationRepository organizationRepository;
        private UserRepository userRepository;

        @Override
        public void onPostLogin() {
            UserProfile profile = TRBAuthenUtil.getInstance().getUserProfile();
            UserProfileMapper userProfileMapper = new UserProfileMapper(profile);

            Organization org = userProfileMapper.getOrganization();
            saveOrUpdate(org);

            User user = userProfileMapper.getUser();
            saveOrUpdate(user);

            AccountUtils.setUser(user);
            setResult(RESULT_OK);
            finish();
        }

        private void saveOrUpdate(Organization org) {
            organizationRepository = BrokerOrganizationRepository.getInstance();
            Organization organization = organizationRepository.findById(org.getOrganizationId());
            if (organization == null) {
                organizationRepository.save(org);
            } else {
                organizationRepository.update(org);
            }
        }

        private void saveOrUpdate(User user) {
            userRepository = BrokerUserRepository.getInstance();
            User userInRepo = userRepository.findByUsername(user.getUsername());
            if (userInRepo == null) {
                userRepository.save(user);
            } else {
                userRepository.update(user);
            }
        }

        @Override
        public void onError(Exception e) {
            throw new RuntimeException(e);
        }
    }
}<commit_msg>ถ้า Authen ไม่ผ่าน  จะขึ้นข้อความว่า กรุณาให้สิทธิเพื่อเข้าใช้งาน ไว้ก่อน<commit_after>/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * 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 th.or.nectec.tanrabad.survey.presenter.authen;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.trb.authen.client.TRBAuthenUtil;
import org.trb.authen.client.TRBCallback;
import org.trb.authen.model.UserProfile;
import th.or.nectec.tanrabad.domain.organization.OrganizationRepository;
import th.or.nectec.tanrabad.domain.user.UserRepository;
import th.or.nectec.tanrabad.entity.Organization;
import th.or.nectec.tanrabad.entity.User;
import th.or.nectec.tanrabad.survey.R;
import th.or.nectec.tanrabad.survey.TanrabadApp;
import th.or.nectec.tanrabad.survey.presenter.AccountUtils;
import th.or.nectec.tanrabad.survey.presenter.TanrabadActivity;
import th.or.nectec.tanrabad.survey.repository.BrokerOrganizationRepository;
import th.or.nectec.tanrabad.survey.repository.BrokerUserRepository;
import th.or.nectec.tanrabad.survey.utils.alert.Alert;
import th.or.nectec.tanrabad.survey.utils.android.CookieUtils;

@SuppressLint("SetJavaScriptEnabled")
public class AuthenActivity extends TanrabadActivity {

    private WebView webView;
    private WebChromeClient webChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            setProgress(newProgress * 1000);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authen);

        CookieUtils.clearCookies(this);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new AuthenWebViewClient());
        webView.setWebChromeClient(webChromeClient);
        webView.getSettings().setJavaScriptEnabled(true);
        loadAuthenticationPage();
    }

    private void loadAuthenticationPage() {
        try {
            String authenUrl = TRBAuthenUtil.getInstance().getAuthorizationUri();
            webView.loadUrl(authenUrl);
        } catch (Exception expect) {
            expect.printStackTrace();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class AuthenWebViewClient extends WebViewClient {

        private static final String PARAM_CODE = "code";
        private static final String AUTHORIZED_CALLBACK_URL = "http://localhost/?";

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith(AUTHORIZED_CALLBACK_URL)) {
                Uri uri = Uri.parse(url);
                String code = uri.getQueryParameter(PARAM_CODE);
                TRBAuthenUtil.getInstance().onAuthorizationResult(code, new AuthenticationCallback());
                webView.stopLoading();
                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onReceivedSslError(WebView view, @NonNull SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    }

    private class AuthenticationCallback implements TRBCallback {

        private OrganizationRepository organizationRepository;
        private UserRepository userRepository;

        @Override
        public void onPostLogin() {
            UserProfile profile = TRBAuthenUtil.getInstance().getUserProfile();
            UserProfileMapper userProfileMapper = new UserProfileMapper(profile);

            Organization org = userProfileMapper.getOrganization();
            saveOrUpdate(org);

            User user = userProfileMapper.getUser();
            saveOrUpdate(user);

            AccountUtils.setUser(user);
            setResult(RESULT_OK);
            finish();
        }

        private void saveOrUpdate(Organization org) {
            organizationRepository = BrokerOrganizationRepository.getInstance();
            Organization organization = organizationRepository.findById(org.getOrganizationId());
            if (organization == null) {
                organizationRepository.save(org);
            } else {
                organizationRepository.update(org);
            }
        }

        private void saveOrUpdate(User user) {
            userRepository = BrokerUserRepository.getInstance();
            User userInRepo = userRepository.findByUsername(user.getUsername());
            if (userInRepo == null) {
                userRepository.save(user);
            } else {
                userRepository.update(user);
            }
        }

        @Override
        public void onError(Exception e) {
            Alert.highLevel().show("คุณจำเป็นต้องให้สิทธิในการเข้าถึงข้อมูลเพื่อเข้าใช้งาน ทันระบาด-สำรวจ");
            TanrabadApp.log(e);
        }
    }
}
{'fraction_non_alphanumeric': 0.08943774761864622, 'fraction_numerical': 0.00202309702436146, 'mean_word_length': 2.9415282392026576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5220951', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3246, 'n_words': 679}